从XML获取标签值

时间:2017-10-17 21:39:08

标签: c# xml unity3d

背景

这是我在线制作的小型XML文件。

<?xml version="1.0"?>
<movement>
    <skill id = "2">
        <cooldown>5</cooldown>
    </skill>
    <skill id = "3">
        <cooldown>10</cooldown>
    </skill>
</movement>

这是我到目前为止尝试解析它的一些代码。

    string dataPath  = Application.dataPath + "/Resources/XML/Skills/";
    DirectoryInfo xmlFolder = new DirectoryInfo (dataPath);
    FileInfo[] files = xmlFolder.GetFiles ("*.xml");

    // Loops through each XML file
    foreach (FileInfo file in files) {
        XmlDocument xdoc = new XmlDocument ();
        xdoc.Load (file.ToString ());
        XmlNodeList nodes = xdoc.DocumentElement.SelectNodes ("/movement");
        foreach (XmlNode node in nodes) { // Movement Layer
            foreach (XmlNode skillNode in node.ChildNodes) {
                print (skillNode.Value);
            }
        }
    }

问题

我可以访问 5和10 值以获得冷却时间,但无法获得&#34; id&#34;技能的价值。我尝试这样做的原因是将技能ID读入我的游戏并存储信息。我几乎用尽了从XmlNode表示的所有方法,例如名称,但它只返回&#34;技能&#34; ,而不是技能的价值,比如2或3.我觉得我在这里遗漏了一些非常简单的东西,但我很难找到正确的术语或措辞来解决这个问题。

3 个答案:

答案 0 :(得分:2)

LINQ To XML会使解析变得更简单......

    static void Main(string[] args)
    {
        var doc = 
@"<?xml version=""1.0""?>
<movement>
<skill id = ""2"" >
     <cooldown> 5 </cooldown>
 </skill>
 <skill id = ""3"" >
      <cooldown> 10 </cooldown>
  </skill>
</movement> ";

        var root = XDocument.Parse(doc);
        foreach (var skill in root.Descendants("skill"))
        {
            Console.WriteLine("Skill: {0} \t CoolDOwn: {1}",
                    (int)skill.Attribute("id"),
                    skill.Element("cooldown").Value);
        }

        Console.ReadLine();
    }

答案 1 :(得分:2)

使用xml linq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var results = doc.Descendants("skill").Select(x => new {
                id = (int)x.Attribute("id"),
                coolDown = (int)x.Element("cooldown")
            }).ToList();
        }
    }
}

答案 2 :(得分:0)

我不会将XMLDocument用于查询目的恕我直言。我发现XDocument是System.Xml.Linq;

的一个更容易的扩展

EG:

using System;
using System.Linq;
using System.Xml.Linq;

namespace ConsoleTester
{
    class Program
    {
        static void Main(string[] args)
        {
            //Here I am mocking your file statically, you could iterate here through a file system.
            var xml =
                "<?xml version=\"1.0\"?><movement><skill id = \"2\"><cooldown>5</cooldown></skill><skill id = \"3\"><cooldown>10</cooldown></skill></movement>";

            //Just parse the file from the text obtained from a StreamReader or similar.
            var xdoc = XDocument.Parse(xml);

            //Chain the events of finding the node(s) you want with 'Elements' then continuing on to more, then you want an Attribute and not a node.  Then select it's value.
            xdoc.Elements("movement").Elements("skill").Attributes("id")
                .Select(x => x.Value)
                .ToList()
                .ForEach(x => Console.WriteLine(x));


            Console.ReadLine();
        }
    }
}