如何读取列表<>使用 C# 从 XML 文件中获取? (基于 XNA 的问题)

发布于 2024-12-11 23:02:53 字数 1860 浏览 0 评论 0 原文

我对 XML 和 XNA 很陌生,并且在读取我输出的列表以存储具有许多不同层的图块地图时遇到了障碍

我的 XML 看起来像这样(只是一个示例):

<XnaContent xmlns:Maps="TileEditorLibrary.Maps">
 <Asset Type="Maps:TileMap">
    <R>
      <Item>
        <C>
          <Item>
            <E>1</E>
            <ID>8</ID>
            <B>8</B>
            <H />
            <T />
          </Item>
          <Item>
            <E>1</E>
            <ID>8</ID>
            <B>8</B>
            <H />
            <T />
          </Item>
        </C>
      </Item>
    </R>
  </Asset>
</XnaContent>

我的代码阅读它看起来像这样,但我不知道接下来该去哪里:

public void LoadContent(XDocument doc, TileMap myMap)
    {
        XDocument mapDoc = doc;
        TileMap map = myMap;

        foreach (XElement element in mapDoc.Element("R").Elements("Item").Elements("C"))
        {
             foreach (XElement mapValue in mapDoc.Element("Item").Elements())
                {
                    if (mapValue.Name.Equals("E"))
                    {
                    //do something
                    }
                    else if (mapValue.Name.Equals("ID"))
                    {
                    }
                    else if (mapValue.Name.Equals("B"))
                    {
                    }
                    else if (mapValue.Name.Equals("H"))
                    {
                    }
                    else if (mapValue.Name.Equals("T"))
                    {
                    }
                }
        }
    }

目前我正在运行应用程序,看看它是否正确获取文档并能够解析信息,但它抛出了一个空对象引用

如果这看起来很正常,我深表歉意含糊不清,但由于我对 XML 缺乏经验,我不确定还有哪些其他信息可以帮助任何潜在的顾问

如果还有其他任何可能有帮助的信息,请询问,我会尽力解释

非常感谢

巴里

I'm quite new to XML and XNA and have ran into a stumbling block in order to read a list That i'm outputting to store a tile map that has numerous different layers

My XML looks like this (just a sample):

<XnaContent xmlns:Maps="TileEditorLibrary.Maps">
 <Asset Type="Maps:TileMap">
    <R>
      <Item>
        <C>
          <Item>
            <E>1</E>
            <ID>8</ID>
            <B>8</B>
            <H />
            <T />
          </Item>
          <Item>
            <E>1</E>
            <ID>8</ID>
            <B>8</B>
            <H />
            <T />
          </Item>
        </C>
      </Item>
    </R>
  </Asset>
</XnaContent>

My code to read it looks something like this but i'm beat as to where to go with it next:

public void LoadContent(XDocument doc, TileMap myMap)
    {
        XDocument mapDoc = doc;
        TileMap map = myMap;

        foreach (XElement element in mapDoc.Element("R").Elements("Item").Elements("C"))
        {
             foreach (XElement mapValue in mapDoc.Element("Item").Elements())
                {
                    if (mapValue.Name.Equals("E"))
                    {
                    //do something
                    }
                    else if (mapValue.Name.Equals("ID"))
                    {
                    }
                    else if (mapValue.Name.Equals("B"))
                    {
                    }
                    else if (mapValue.Name.Equals("H"))
                    {
                    }
                    else if (mapValue.Name.Equals("T"))
                    {
                    }
                }
        }
    }

At the moment i'm running the app to see that it is getting the doc alright and able to parse the information but it is throwing a null object reference

I apologise if this seems quite vague but due to my inexperience with XML i'm not sure what other info might help any prospective advisor

If there is anything else that might help please ask and i'll try to explain

Many thanks

Barry

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉态萌生 2024-12-18 23:02:53

您应该做的是阅读 XML 序列化和反序列化
这样您就可以创建一个表示 XML 的类并直接反序列化它,而无需任何 XML 解析。

这里是一个如何执行此操作的示例。

What you should do is read up on XML serialization and deserialization.
That way you can create a class which represents the XML and deserialize it directly without any XML parsing.

Here is an example how to do it.

不语却知心 2024-12-18 23:02:53

这可能会给您一些思考:

public void LoadContent(XDocument doc, TileMap myMap)
{
    var lookup = new Dictionary<string, Action<string>>()
    {
        { "E", v => { Console.WriteLine("E  = " + v); } },
        { "ID", v => { Console.WriteLine("ID = " + v); } },
        { "B", v => { Console.WriteLine("B  = " + v); } },
        { "H", v => { Console.WriteLine("H  = " + v); } },
        { "T", v => { Console.WriteLine("T  = " + v); } },
    };

    var actions =
        from e in doc.Root
            .Element("Asset")
            .Element("R")
            .Elements("Item")
            .Elements("C")
        from mv in e
            .Elements("Item")
            .Elements()
        let name = mv.Name.ToString()
        let value = mv.Value
        select new Action(() => lookup[name](value));

    foreach (var a in actions)
        a.Invoke();
}

This might give you some food for thought:

public void LoadContent(XDocument doc, TileMap myMap)
{
    var lookup = new Dictionary<string, Action<string>>()
    {
        { "E", v => { Console.WriteLine("E  = " + v); } },
        { "ID", v => { Console.WriteLine("ID = " + v); } },
        { "B", v => { Console.WriteLine("B  = " + v); } },
        { "H", v => { Console.WriteLine("H  = " + v); } },
        { "T", v => { Console.WriteLine("T  = " + v); } },
    };

    var actions =
        from e in doc.Root
            .Element("Asset")
            .Element("R")
            .Elements("Item")
            .Elements("C")
        from mv in e
            .Elements("Item")
            .Elements()
        let name = mv.Name.ToString()
        let value = mv.Value
        select new Action(() => lookup[name](value));

    foreach (var a in actions)
        a.Invoke();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文