C# 在c中从xml获取属性名#
我得到的XML文件如下所示:C# 在c中从xml获取属性名#,c#,xml,C#,Xml,我得到的XML文件如下所示: <list> <sublist id1="a" id2="b" id3="b" id4="b"> <item name="name1"> <property1>a</property1> </item> <item2 name="name2"> <property1>c</property1>
<list>
<sublist id1="a" id2="b" id3="b" id4="b">
<item name="name1">
<property1>a</property1>
</item>
<item2 name="name2">
<property1>c</property1>
</item2>
</sublist>
<sublist id1="b" id2="b" id3="a" id4="b">
[...more XML here...]
</sublist>
A.
C
[…这里有更多XML…]
如何找到值为“a”的元素“sublist”的属性名 如果XML字符串位于名为
XML
的变量中:
XDocument
.Parse(xml) // parse the XML
.Descendants() // get all descendant elements
.SelectMany(e => e.Attributes()) // get all attributes from all elements
.Where(a => a.Value == "a") // leave only attributes whose value is "a"
.Select(a => a.Name); // select the name of those attributes
结果是:
id1
id3
请注意,此代码使用XElement
和LINQ来实现目标。还有许多其他方法,其中一些可能更适合您的需要
更新
我注意到现在您只在子列表
元素上查找属性。可以修改代码以处理以下情况:
XDocument
.Parse(xml) // parse the XML
.Descendants("sublist") // get all descendant elements named "sublist"
.SelectMany(e => e.Attributes()) // get all attributes from all elements
.Where(a => a.Value == "a") // leave only attributes whose value is "a"
.Select(a => a.Name); // select the name of those attributes
区别在于对子体的调用
,它现在过滤掉所有未被称为子列表
的元素
在一条评论中,您还询问了当只有一个子列表
元素时如何处理这种情况。上面的代码片段在这方面也很好,因为它们没有对元素的数量做任何假设
您可能会尝试以不同的方式处理该情况,例如使用以下代码:
XDocument
.Parse(xml) // parse the XML
.Descendants("sublist") // get all descendant elements named sublist
.Single() // get the one and only element
.Attributes() // get all attributes from all elements
.Where(a => a.Value == "a") // leave only attributes whose value is "a"
.Select(a => a.Name) // select the name of those attributes
与前面的示例不同的是,我们在这里使用Single
从结果中提取一个且唯一的子列表
元素。此时代码中的项类型变为XElement
,如果子列表中没有(“序列不包含元素”)或多个(“序列包含多个元素”)元素,则Single
将引发异常。在下一行中,我们可以摆脱SelectMany
调用,直接访问XElement
的属性
但是在我看来,代码中的更改不值得您失去健壮性。如果您在名为XML
的变量中有XML字符串:
XDocument
.Parse(xml) // parse the XML
.Descendants() // get all descendant elements
.SelectMany(e => e.Attributes()) // get all attributes from all elements
.Where(a => a.Value == "a") // leave only attributes whose value is "a"
.Select(a => a.Name); // select the name of those attributes
结果是:
id1
id3
请注意,此代码使用XElement
和LINQ来实现目标。还有许多其他方法,其中一些可能更适合您的需要
更新
我注意到现在您只在子列表
元素上查找属性。可以修改代码以处理以下情况:
XDocument
.Parse(xml) // parse the XML
.Descendants("sublist") // get all descendant elements named "sublist"
.SelectMany(e => e.Attributes()) // get all attributes from all elements
.Where(a => a.Value == "a") // leave only attributes whose value is "a"
.Select(a => a.Name); // select the name of those attributes
区别在于对子体的调用
,它现在过滤掉所有未被称为子列表
的元素
在一条评论中,您还询问了当只有一个子列表
元素时如何处理这种情况。上面的代码片段在这方面也很好,因为它们没有对元素的数量做任何假设
您可能会尝试以不同的方式处理该情况,例如使用以下代码:
XDocument
.Parse(xml) // parse the XML
.Descendants("sublist") // get all descendant elements named sublist
.Single() // get the one and only element
.Attributes() // get all attributes from all elements
.Where(a => a.Value == "a") // leave only attributes whose value is "a"
.Select(a => a.Name) // select the name of those attributes
与前面的示例不同的是,我们在这里使用Single
从结果中提取一个且唯一的子列表
元素。此时代码中的项类型变为XElement
,如果子列表中没有(“序列不包含元素”)或多个(“序列包含多个元素”)元素,则Single
将引发异常。在下一行中,我们可以摆脱SelectMany
调用,直接访问XElement
的属性
但在我看来,代码中的更改不值得您失去健壮性。哪个属性,哪个值?OP正在查找id1
,基于这是具有值a
的属性的名称这一事实。应该是一个相当简单的Linq2XML查询。到目前为止,您的代码在哪里?XML库并不短缺,答案将与您选择并正在使用的库相关联。哪个属性,哪个值?OP正在查找id1
,基于这一事实,即具有值a
的属性的名称。应该是一个相当简单的Linq2XML查询。到目前为止,您的代码在哪里?XML库并不短缺,答案将与您选择并正在使用的库相关联。首先感谢您的帮助@Frank:如果我的XML文件中只有元素“sublist”一次,我如何转换查询的答案?@Frank您给我的答案几乎就是解决方案(感谢您的努力)-还有一个问题:我如何从中生成一个字符串?请注意您的意思(如果您也共享一些您编写的代码,这将非常有帮助)。但是.NET中的任何对象都可以通过调用其ToString()
方法转换为字符串。首先感谢您的帮助@Frank:如果我的XML文件中只有一个元素“sublist”,我如何转换查询的答案呢?@Frank您给我的答案几乎就是解决方案(感谢您的努力)-还有一个问题:我如何从中生成一个字符串?请注意您的意思(如果您也共享一些您编写的代码,这将非常有帮助)。但是.NET中的任何对象都可以通过调用其ToString()
方法转换为字符串。