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

发布于 2024-11-10 04:35:41 字数 333 浏览 1 评论 0原文

我有以下 XML 文档:

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

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

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

关于如何执行此操作的任何想法?

I have the following XML document:

<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:

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

Any ideas on how I can do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

波浪屿的海角声 2024-11-17 04:35:41

我在 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);

I tried this out in LINQPad and it provides what you are looking for:

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);
拿命拼未来 2024-11-17 04:35:41

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

XDocument doc = ...;
var dict = doc.Root
    .Elements()
    .ToDictionary(
        e => e.Name.ToString(),
        e => e.Attribute("value").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);
花开半夏魅人心 2024-11-17 04:35:41
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);
}
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);
}
娇纵 2024-11-17 04:35:41

您可以使用 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 到 xml非常容易且易于管理
http://www.codeproject.com/KB/linq/LINQtoXML.aspx

You can use xmldocument/ xmtextreader object use these links these will help

http://msdn.microsoft.com/en-us/library/c445ae5y(v=vs.80).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

メ斷腸人バ 2024-11-17 04:35:41

像这样的东西

XDocument doc = XDocument.Parse(xmlText);
IDictionary<string,string> dic = doc.Elements("parameters").ToDictionary(e => e.Name.LocalName, e => e.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

翻身的咸鱼 2024-11-17 04:35:41
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);
    }
}
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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文