Question

Is there a way to get a list of the values of the attributes for specific b (for example r4)?
List<string> that contains: "p3e3" and "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" />"

Était-ce utile?

La solution

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:

var result = xdoc.XPathSelectElements("a/b[@id='r4']/P/Pr")
                 .Select(pr => (string)pr.Attribute("id"));
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top