解析具有相同名称的Xml节点

时间:2015-07-16 13:19:15

标签: c# xml parsing

当我尝试解析此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

0 个答案:

没有答案