[英]Get a name attribute values from multiple inner elements od XML File into C#
In following code I want to get the name attribute value of all the inner elements of <List>
which are <InnerList>
.在下面的代码中,我想获取
<List>
的所有内部元素的name属性值,它们是<InnerList>
。 The requirement is to store the attribute name values to the array in c# code.要求是将属性名称值存储到 C# 代码中的数组中。
<List>
<InnerList name="abc">
<element1></element1>
<element2></element2>
</InnerList>
<InnerList name="xyz">
<element1></element1>
<element2></element2>
</InnerList >
</List>
The required output is:所需的输出是:
output = abc, xyz
The Output variable should contain the values of name attribute of all <InnerList>
elements is array format which can be displayed via foreach loop. Output 变量应包含所有
<InnerList>
元素的 name 属性值,数组格式可通过 foreach 循环显示。
You can try using XDocument
您可以尝试使用
XDocument
var xdoc = XDocument.Load(fileName);
var result = string.Join(", ", xdoc.Descendants("InnerList")
.Select(x => x.Attribute("name").Value));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.