編輯:我正在編輯這個問題,因爲它是我唯一的低票問題,我試圖得到一個問題禁止解除。使用xml文件在c中創建對象#
我目前正在使用基於文本的遊戲的xml文件。我已經寫過這個文件,而且我知道如何從文件中讀取數據,但是我很困惑如何從文件中創建實際的對象。我的問題是如何從xml文件轉移到實際的C#對象。我正在尋找直接代碼或者一個很好的教程(我已經廣泛搜索並且無法找到它)。下面是我的xml文件和我的C#代碼的樣本。
<?xml version="1.0" encoding="utf-8" ?>
<rooms>
<room>
<entrance>
<Description>
<Entry1>
You walk up and try the door bell. No answer...You force your way into the large, heavy, oak door. In the main foyer, you take
a quick look around. You see a baseball bat by the door and a Super Snack on a nearby dresser. Carved into one of the walls are the
words "First is 0." There is a door to the west and a door to the north.
</Entry1>
<Entry2>
You are back in the main foyer. Carved into one of the walls are the words "First is 0." There are doors to the west, north, or east.
</Entry2>
<Entry3>
You are back in the main foyer. You see a baseball bat by the door. Carved into one of the walls are the words "First is 0."
There are doors to the west, north, and east.
</Entry3>
<Entry4>
You are back in the main foyer. You see a Super Snack on a nearby dresser. Carved into one of the walls are the words "First is 0." There
are doors to the west, north, and east.
</Entry4>
</Description>
<Items>
<Item name ="Baseball Bat" type ="weapon" attribute="player.attack + 2"></Item>
<Item name ="Super Snack" type ="consumable" attribute ="player.health = 100"></Item>
</Items>
<border>
<direction>north</direction>
<name>room2</name>
</border>
<border>
<direction>west</direction>
<name>room3</name>
</border>
</entrance>
</room>
<room>
<room2>
<Description>
<Locked>You walk forward and try the door. Locked...maybe find a key?</Locked>
<Unlocked>
You use the key you found on the door in front of you. It fits! In the next room you find two doors: one to the west, one to the east.
There is a picture on the wall of the professor receiving a watch from a colleague, looks like maybe a work anniversary gift.
</Unlocked>
</Description>
<border>
<direction>west</direction>
<name>room4</name>
</border>
<border>
<direction>east</direction>
<name>room9</name>
</border>
</room2>
</room>
這是我的Rooms文件C#代碼。沒有太多,因爲我不確定我需要什麼。
using System.Xml; 命名空間FirstTextBasedGame { class Rooms { string description; 字符串寄存器; 公共房間(){ // 不確定哪裏何去何從...... }} }
只是要清楚,項目和房間要應用的問題兩個不同的實體? – Mfusiki