【问题标题】:XDocument get all nodes with attributesXDocument 获取所有具有属性的节点
【发布时间】:2011-05-27 06:43:26
【问题描述】:

我有以下 XML 文档:

<parameters>
    <source value="mysource" />
    <name value="myname" />
    <id value="myid" />
</parameters>

我正在尝试使用 XDocument 解析此 XML,以便获得包含节点及其值的列表(字典):

来源 => 我的来源, 名字 => 我的名字, id => myid

关于如何做到这一点的任何想法?

【问题讨论】:

    标签: c# xml linq-to-xml


    【解决方案1】:

    我在 LINQPad 中进行了尝试,它提供了您正在寻找的内容:

    string xml = @"<parameters>
      <source value=""mysource"" />
      <name value=""myname"" />
      <id value=""myid"" />
    </parameters>";
    
    var doc = XDocument.Parse(xml);
    IDictionary dict = doc.Element("parameters")
      .Elements()
      .ToDictionary(
        d => d.Name.LocalName, // avoids getting an IDictionary<XName,string>
        l => l.Attribute("value").Value);
    

    【讨论】:

      【解决方案2】:

      如果您有一个包含您在此处显示的内容的文档,这应该可以:

      XDocument doc = ...;
      var dict = doc.Root
          .Elements()
          .ToDictionary(
              e => e.Name.ToString(),
              e => e.Attribute("value").Value);
      

      【讨论】:

        【解决方案3】:
        XDocument x = XDocument.Parse(
                    @"<parameters>
                        <source value=""mysource"" />
                        <name value=""myname"" />
                        <id value=""myid"" />
                    </parameters>");
        
        var nodes = from elem in x.Element("parameters").Elements()
                    select new { key = elem.Name.LocalName, value = elem.Attribute("value").Value };
        
        var list = new Dictionary<string, string>();
        foreach(var node in nodes)
        {
            list.Add(node.key, node.value);
        }
        

        【讨论】:

        • 这个版本在逻辑上等同于 ToDictionary 方法,但性能特征略有不同。
        【解决方案4】:

        您可以使用 xmldocument/ xmtextreader 对象使用这些链接,这些会有所帮助

        http://msdn.microsoft.com/en-us/library/c445ae5y(v=vs.80).aspx

        http://www.c-sharpcorner.com/uploadfile/mahesh/readwritexmltutmellli2111282005041517am/readwritexmltutmellli21.aspx

        但我强烈建议如果可能的话,使用 linq to xml 非常容易和易于管理 http://www.codeproject.com/KB/linq/LINQtoXML.aspx

        【讨论】:

          【解决方案5】:

          类似的东西

          XDocument doc = XDocument.Parse(xmlText);
          IDictionary<string,string> dic = doc.Elements("parameters").ToDictionary(e => e.Name.LocalName, e => e.Value);
          

          希望对你有帮助

          【讨论】:

            【解决方案6】:
            using System;
            using System.Linq;
            using System.Xml.Linq;
            using System.Collections.Generic;
            
            class Program{
                static void Main(){
                    var doc = XDocument.Load("1.xml");
                    var result = (from node in doc.Root.Elements()
                                 select new{ Key = node.Name, Value = node.Attribute("value").Value})
                                 .ToDictionary(p =>p.Key, p=>p.Value);
                    foreach(var p in result) Console.WriteLine("{0}=>{1}", p.Key, p.Value);
                }
            }
            

            【讨论】:

              最近更新 更多