【问题标题】:using xml files to create objects in c#使用xml文件在c#中创建对象
【发布时间】: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


【解决方案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>

这种方法的优点是现在您可以创建一个序列化(able)类Item,您可以在房间和库存系统中使用它。

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

你可能会觉得有趣:

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

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

【讨论】:

  • 这正是我想要的!谢谢!
  • 对此还有一个问题。我阅读了您的链接,并且还阅读了有关序列化的信息。我了解此代码的工作原理:
  • 对此还有一个问题。我阅读了您的链接,并且还阅读了有关序列化的信息。我理解这里显示的代码是如何工作的:msdn.microsoft.com/en-us/library/mt656717.aspx 但是,如果您在 Room 类型的房间中有多个对象,并且您实例化了该房间,是同时创建所有这些对象,还是必须创建每个对象?单独?以及如何从一系列房间中调用特定房间?
  • @ethancodes 你的房间有名字吗?在最简单的级别上,使用这些名称来区分它们。或者使用其他标识符,以便您可以区分另一个。同样的项目。所有的“刀”都是一样的,还是有些锋利与钝。 PS,在Mfusiki的房间顶部,他有一个id值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-15
  • 1970-01-01
  • 2011-02-26
  • 1970-01-01
  • 2015-09-23
  • 1970-01-01
相关资源
最近更新 更多