使用相同名称解析 XML 节点

3

当我尝试解析此XML时,遇到了问题。

<?xml version="1.0" encoding="ISO-8859-1"?>
<Objects>
  <Object type="0x141" id="Amulet of Resurrection">
    <Class>Equipment</Class>
    <Item />
    <ActivateOnEquip stat="20" amount="2">IncrementStat</ActivateOnEquip>
    <ActivateOnEquip stat="21" amount="2">IncrementStat</ActivateOnEquip>
    <ActivateOnEquip stat="22" amount="2">IncrementStat</ActivateOnEquip>
    <ActivateOnEquip stat="26" amount="2">IncrementStat</ActivateOnEquip>
    <ActivateOnEquip stat="27" amount="2">IncrementStat</ActivateOnEquip>
    <ActivateOnEquip stat="28" amount="2">IncrementStat</ActivateOnEquip>
    <ActivateOnEquip stat="0" amount="20">IncrementStat</ActivateOnEquip>
    <ActivateOnEquip stat="3" amount="20">IncrementStat</ActivateOnEquip>
  </Object>
</Objects>

我的程序只读取了第一个ActivateOnEquip,但我希望它能读取所有的。

这是我的程序:

using System;
using System.Xml;

namespace ParseXML
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            ParseXMLEquip(@"..\..\obj\Debug\Object\Test.xml");
            Console.ReadLine();
        }

        private static void ParseXMLEquip(string fileName)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(fileName);
            XmlNodeList EquipNodes = xmlDocument.SelectNodes("//*");
            {
                foreach (XmlNode EquipNode in EquipNodes)
                {
                    XmlNode Stat = EquipNode["ActivateOnEquip"];

                    if (Stat != null)
                    {
                        switch (Stat.Attributes["stat"].InnerText)
                        {
                            case "0":
                            {
                                Console.WriteLine("Hp: " + Stat.Attributes["amount"].InnerText);
                            }
                                break;
                            case "3":
                            {
                                Console.WriteLine("Mp: " + Stat.Attributes["amount"].InnerText);
                            }
                                break;
                            case "20":
                            {
                                Console.WriteLine("Attack: " + Stat.Attributes["amount"].InnerText);
                            }
                                break;
                            case "21":
                            {
                                Console.WriteLine("Defense: " + Stat.Attributes["amount"].InnerText);
                            }
                                break;
                            case "22":
                            {
                                Console.WriteLine("Speed: " + Stat.Attributes["amount"].InnerText);
                            }
                                break;
                            case "28":
                            {
                                Console.WriteLine("Dexterity: " + Stat.Attributes["amount"].InnerText);
                            }
                                break;
                            case "26":
                            {
                                Console.WriteLine("Vitality: " + Stat.Attributes["amount"].InnerText);
                            }
                                break;
                            case "27":
                            {
                                Console.WriteLine("Wisdom: " + Stat.Attributes["amount"].InnerText);
                            }
                                break;
                        }
                    }
                }
            }
        }
    }
}

控制台只会输出 Attack : 2,但我想要这样的结果:

Attack : 2 
Defense : 2 
Speed : 2 
Vitality : 2 
Wisdom : 2 
Dexterity : 2 
HP : 20
MP : 20
1个回答

3
使用Linq2Xml怎么样?
Dictionary<string, string> mapping = new Dictionary<string, string>()
{
    {"0","Hp"},
    {"3","Mp"},
    {"20","Attack"},
    {"21","Defence"},
    {"22","Speed"},
    {"28","Dexterity"},
    {"26","Vitality"},
    {"27","Wisdom"},
};

var result = XDocument.Load(filename)
             .Descendants("ActivateOnEquip")
             .Select(x => new { stat = x.Attribute("stat").Value, amount = x.Attribute("amount").Value })
             .ToList();

foreach(var item in result)
{
    Console.WriteLine( mapping[item.stat] + ": " + item.amount);
}

我能在这段代码中添加像元素<Class>这样的东西吗?我尝试过了,但是遇到了之前相同的问题。非常感谢@EZI。 - Belley

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接