使用xml文件在c#中创建对象

时间:2016-07-06 17:21:41

标签: c# xml visual-studio

编辑:我正在编辑这个问题,因为这是我唯一的一个投票问题,而我正试图解除一个问题。

我目前正在使用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#代码。没有多少,因为我不确定我在这里需要什么。

使用System.Xml; 名称空间FirstTextBasedGame {     课堂客房     {         字符串描述;         弦寄宿生;         公共房间()         {             //不确定从哪里开始......         }     } }

1 个答案:

答案 0 :(得分:1)

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>

这种方法有什么好处,现在您可以创建一个序列化(能够)类Item,您可以在房间和库存系统中使用它。

因此可以轻松地将项目从一个对象转移到PlayerInventoryRoom,反之亦然。您还可以将整个房间状态以及库存保存到XML文件中。使保存和加载游戏状态不那么痛苦。

你可能会觉得有趣的事情:

http://answers.unity3d.com/questions/443525/serialize-a-gameobject-including-components.html

https://msdn.microsoft.com/en-us/library/bb203924.aspx