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

时间:2021-10-20 07:54:58

I have XElement object formated like this :

我有这样的XElement对象:

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

我有很多这样的节点,所以我想知道是否有简单的方法可以通过'[]'或某种方式访问这些属性。

2 个解决方案

#1


4  

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

#2


1  

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

这将打印出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:

如果你只需要PatientFieldSetting节点过滤器的名称:

from node in doc.Descendants("PatientFieldSetting")

#1


4  

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

#2


1  

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

这将打印出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:

如果你只需要PatientFieldSetting节点过滤器的名称:

from node in doc.Descendants("PatientFieldSetting")