如何从嵌套的 xml 节点获取属性值?

发布于 2024-10-09 10:52:29 字数 623 浏览 4 评论 0原文

我的 XElement 对象格式如下:

<Setting guid="3bcedf55-b75f-456b-b90a-a92cbbb022ga">
    <PatientFieldList>
        <PatientFieldSetting PatientName="UserDecision" PatentFieldLength="64" />
        <PatientFieldSetting PatientName="prohibited" PatentFieldLength="128" />
    </PatientFieldList>
</Setting>

我必须获取所有节点中所有属性的值,但我不知道如何:/我尝试过

xml.Elements("PatientFieldList")

xml.Descendants("PatientsSettingsFieldsList").Where(x => x.Attribute("PatentFieldLength").Value == 64)`

我有很多这样的节点,所以我想知道是否有简单的方法来访问这些节点通过“[]”或其他方式属性。

I have XElement object formated like this :

<Setting guid="3bcedf55-b75f-456b-b90a-a92cbbb022ga">
    <PatientFieldList>
        <PatientFieldSetting PatientName="UserDecision" PatentFieldLength="64" />
        <PatientFieldSetting PatientName="prohibited" PatentFieldLength="128" />
    </PatientFieldList>
</Setting>

I have to get values of all attributes in all nodes but I don't know how :/ I tried

xml.Elements("PatientFieldList")

xml.Descendants("PatientsSettingsFieldsList").Where(x => x.Attribute("PatentFieldLength").Value == 64)`

I have a lot of node like that so i wonder if there is easy way to access to these attribute by '[]' or somehow.

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

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

发布评论

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

评论(2

夜无邪 2024-10-16 10:52:29

代码:

using System;
using System.Linq;
using System.Xml.Linq

var xml = "<Setting ...";
var doc = XElement.Parse(xml);
int i; // for int parse
var q = from node in doc.Descendants("PatientFieldSetting")
        let name = node.Attribute("PatientName")
        let length = node.Attribute("PatentFieldLength")
        select new { Name = (name != null) ? name.Value : "", Length = (length != null && Int32.TryParse(length.Value, out i)) ? i : 0 };

foreach (var node in q)
{
    Console.WriteLine("Name={0}, Length={1}", node.Name, node.Length);
}

输出:

Name=UserDecision, Length=64
Name=prohibited, Length=128

Code:

using System;
using System.Linq;
using System.Xml.Linq

var xml = "<Setting ...";
var doc = XElement.Parse(xml);
int i; // for int parse
var q = from node in doc.Descendants("PatientFieldSetting")
        let name = node.Attribute("PatientName")
        let length = node.Attribute("PatentFieldLength")
        select new { Name = (name != null) ? name.Value : "", Length = (length != null && Int32.TryParse(length.Value, out i)) ? i : 0 };

foreach (var node in q)
{
    Console.WriteLine("Name={0}, Length={1}", node.Name, node.Length);
}

Output:

Name=UserDecision, Length=64
Name=prohibited, Length=128
一曲琵琶半遮面シ 2024-10-16 10:52:29

这将打印出 xml 中具有属性的所有节点的属性:

XDocument doc = //your data

var q = from node in doc.Descendants()
        where node.Attributes().Count() > 0
        select new {NodeName = node.Name, Attributes = node.Attributes()};

foreach (var node in q)
{
    Console.WriteLine( node.NodeName );
    foreach (var attribute in node.Attributes)
    {
        Console.WriteLine(attribute.Name + ":" + attribute.Value);
    }
    Console.WriteLine();
}

如果您只希望 PatientFieldSetting 节点过滤名称:

from node in doc.Descendants("PatientFieldSetting")

This will print out attributes of all nodes which have attributes in your xml:

XDocument doc = //your data

var q = from node in doc.Descendants()
        where node.Attributes().Count() > 0
        select new {NodeName = node.Name, Attributes = node.Attributes()};

foreach (var node in q)
{
    Console.WriteLine( node.NodeName );
    foreach (var attribute in node.Attributes)
    {
        Console.WriteLine(attribute.Name + ":" + attribute.Value);
    }
    Console.WriteLine();
}

If you only want PatientFieldSetting nodes filter for the name:

from node in doc.Descendants("PatientFieldSetting")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文