简体   繁体   English

获取xml中的属性值列表

[英]get a list of attribute values in xml

Is there a way to get a list of the values of the attributes for specific b (for example r4)? 有没有一种方法可以获取特定b的属性值列表(例如r4)?
List<string> that contains: "p3e3" and "p3e4" List<string>包含:“ p3e3”和“ p3e4”

<a>
  <b id="r4" Name="b 4">
    <P>
      <Pr id="p3e3" />
      <Pr id="p3e4" />
    </P>
  </b>
  <b id="r5" Name="b 5">
     ....
  </b>
</a>  

The most I got was a List<XElement> that contained " <Pr id="p3e3" /> " and " <Pr id="p3e4" /> " 我得到的最多是一个List<XElement> ,其中包含“ <Pr id="p3e3" /> “和” <Pr id="p3e4" />

var xdoc = XDocument.Load(path_to_xml);
var result = xdoc.Root.Elements("b")
                 .Where(b => (string)b.Attribute("id") == "r4")
                 .SelectMany(b => b.Element("P").Elements("Pr"))
                 .Select(pr => (string)pr.Attribute("id"));

Or with XPath: 或使用XPath:

var result = xdoc.XPathSelectElements("a/b[@id='r4']/P/Pr")
                 .Select(pr => (string)pr.Attribute("id"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM