【发布时间】:2016-07-06 17:21:40
【问题描述】:
编辑:我正在编辑这个问题,因为这是我唯一被否决的问题,我正在努力解除问题禁令。
我目前正在为基于文本的游戏使用 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>
这是我的房间文件 C# 代码。没有太多,因为我不确定我在这里需要什么。
使用 System.Xml; 命名空间 FirstTextBasedGame { 教室 { 字符串描述; 字符串寄宿生; 公共房间() { //不确定从这里去哪里... } } }
【问题讨论】:
-
为了清楚起见,项目和房间是您要向其应用问题的两个不同实体?
标签: c# xml visual-studio