简体   繁体   English

XDocument 获取所有具有属性的节点

[英]XDocument get all nodes with attributes

I have the following XML document:我有以下 XML 文档:

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

I'm trying to parse this XML, using XDocument so that I would get a list (Dictionary) containing the node and it's value:我正在尝试使用 XDocument 解析此 XML,以便获得包含节点及其值的列表(字典):

source => mysource,
name => myname,
id => myid

Any ideas on how I can do this?关于如何做到这一点的任何想法?

I tried this out in LINQPad and it provides what you are looking for:我在 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);

Something like this像这样的东西

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

Hope this helps希望这可以帮助

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

Provided you have a document with the content you show here, this should work:如果您有一个包含您在此处显示的内容的文档,则应该可以:

XDocument doc = ...;
var dict = doc.Root
    .Elements()
    .ToDictionary(
        e => e.Name.ToString(),
        e => e.Attribute("value").Value);
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);
}

You can use xmldocument/ xmtextreader object use these links these will help您可以使用 xmldocument/ xmtextreader object 使用这些链接这些将有所帮助

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

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

but i strongly suggest if possible use linq to xml that is far easy and manageable http://www.codeproject.com/KB/linq/LINQtoXML.aspx但我强烈建议如果可能的话,使用 linq 到 xml 这很容易和易于管理http://www.codeproject.com/KB/linq/LINxQtoXML.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM