C# 使用xml文件在c中创建对象#
编辑:我正在编辑这个问题,因为这是我唯一一个被否决的问题,我正试图取消一个问题禁令 我目前正在使用一个xml文件进行基于文本的游戏。我已经写了这个文件,我知道如何从文件中读取,但是我对如何从文件中创建实际对象感到困惑。我的问题是如何从xml文件传输到实际的C#对象。我正在寻找直接的代码,或者一个好的教程(我已经广泛地搜索了其中的一个,但找不到)。下面是我的xml文件和C#代码的示例C# 使用xml文件在c中创建对象#,c#,xml,visual-studio,C#,Xml,Visual Studio,编辑:我正在编辑这个问题,因为这是我唯一一个被否决的问题,我正试图取消一个问题禁令 我目前正在使用一个xml文件进行基于文本的游戏。我已经写了这个文件,我知道如何从文件中读取,但是我对如何从文件中创建实际对象感到困惑。我的问题是如何从xml文件传输到实际的C#对象。我正在寻找直接的代码,或者一个好的教程(我已经广泛地搜索了其中的一个,但找不到)。下面是我的xml文件和C#代码的示例 <?xml version="1.0" encoding="utf-8" ?> <ro
<?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>
你走上去试试门铃。没有回答……你强行进入那扇又大又重的橡木门。在大堂里,你坐
快速环顾四周。你会在门口看到一个棒球棒,在附近的梳妆台上看到一份超级零食。其中一面墙上刻着
单词“第一个是0”。有一扇门通向西方,一扇门通向北方。
你回到大堂了。其中一面墙上刻着“第一个是0”的字样。有门通向西、北或东。
你回到大堂了。你看到门边有一个棒球棒。其中一面墙上刻着“第一个是0”
西面、北面和东面都有门。
你回到大堂了。你会在附近的梳妆台上看到一种超级零食。其中一面墙上刻着“第一个是0”的字样
是通向西方、北方和东方的门。
北
房间2
西
3号房间
你向前走,试着开门。锁上了…也许能找到钥匙?
你用你在门上找到的钥匙。它合适!在隔壁房间里,你会发现两扇门:一扇向西,一扇向东。
墙上有一张照片,教授从同事那里收到一块手表,看起来像是工作周年纪念礼物。
西
4号房间
东
9号房间
这是我的房间文件C#代码。没有太多,因为我不确定我需要什么
使用System.Xml;
命名空间FirstTextBasedName
{
教室
{
字符串描述;
线板;
公共房间()
{
//不知道从这里到哪里去。。。
}
}
}XML应该可以完美地工作。许多游戏开发人员(仅根据我自己的经验)使用XML和序列化来定义和创建此类实体 你可以有这样的东西:
<room id="3203jfjb" width="10" height="40">
<items>
<item name="table">
</item>
<item name="chest" type="container">
<containerItems>
<item name="knife">
</item>
</containerItems>
</item>
</items>
</room>
该方法的优点在于,现在您可以创建一个序列化(able)类Item
,可以在两个房间和库存系统中使用
因此,可以轻松地将物品从一个对象,即播放器库
转移到房间
,反之亦然。您还可以将整个房间状态以及库存保存到XML文件中。使保存和加载游戏状态的痛苦小得多
您可能会发现一些有趣的事情:
请澄清,物品和房间是两个不同的实体,您要将问题应用到这两个实体?这正是我想要的!非常感谢。还有一个问题。我读了你们的链接,也读了关于序列化的内容。我理解这段代码的工作原理:还有一个问题。我读了你们的链接,也读了关于序列化的内容。我理解这里显示的代码是如何工作的:但是如果在类型为room的房间中有多个对象,并且实例化了该房间,那么是同时创建所有这些对象,还是必须单独创建每个对象?我如何从一系列房间中命名一个特定的房间?你的房间有名字吗?在最简单的层次上,使用这些名称来区分它们。或者使用一些其他的标识符,这样你就可以区分它们了。同样,这些项目也是如此。所有的“刀”都是一样的,或者是一些锋利的与迟钝的。另外,在Mfusiki房间的顶部,他有一个id值。