草庐IT

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

coder 2024-06-30 原文

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

最佳答案

使用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);
}

关于c# - 解析具有相同名称的 Xml 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31455373/

有关c# - 解析具有相同名称的 Xml 节点的更多相关文章

  1. Ruby 解析字符串 - 2

    我有一个字符串input="maybe(thisis|thatwas)some((nice|ugly)(day|night)|(strange(weather|time)))"Ruby中解析该字符串的最佳方法是什么?我的意思是脚本应该能够像这样构建句子:maybethisissomeuglynightmaybethatwassomenicenightmaybethiswassomestrangetime等等,你明白了......我应该一个字符一个字符地读取字符串并构建一个带有堆栈的状态机来存储括号值以供以后计算,还是有更好的方法?也许为此目的准备了一个开箱即用的库?

  2. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  3. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  4. ruby - 用逗号、双引号和编码解析 csv - 2

    我正在使用ruby​​1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\

  5. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  6. ruby - 如果指定键的值在数组中相同,如何合并哈希 - 2

    我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat

  7. c# - 如何在 ruby​​ 中调用 C# dll? - 2

    如何在ruby​​中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL

  8. C# 到 Ruby sha1 base64 编码 - 2

    我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha

  9. ruby-on-rails - 我更新了 ruby​​ gems,现在到处都收到解析树错误和弃用警告! - 2

    简而言之错误:NOTE:Gem::SourceIndex#add_specisdeprecated,useSpecification.add_spec.Itwillberemovedonorafter2011-11-01.Gem::SourceIndex#add_speccalledfrom/opt/local/lib/ruby/site_ruby/1.8/rubygems/source_index.rb:91./opt/local/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/rails/gem_dependency.rb:275:in`==':und

  10. ruby-on-rails - Rails 3.1 中具有相同形式的多个模型? - 2

    我正在使用Rails3.1并在一个论坛上工作。我有一个名为Topic的模型,每个模型都有许多Post。当用户创建新主题时,他们也应该创建第一个Post。但是,我不确定如何以相同的形式执行此操作。这是我的代码:classTopic:destroyaccepts_nested_attributes_for:postsvalidates_presence_of:titleendclassPost...但这似乎不起作用。有什么想法吗?谢谢! 最佳答案 @Pablo的回答似乎有你需要的一切。但更具体地说...首先改变你View中的这一行对此#

随机推荐