I need a list of all the Attribute ID(the value) of Descendants(Frame) that have an Attribute SecondFeature (Descendants-ObjectClass) that equal Vehicle. (there is node that have 1 "object", other 2/3 time and other not at all) this is a part of the code:
<?xml version="1.0" encoding="utf-8" ?>
- <Frame ID="120">
<PTZData Zoom="1.000" />
- <Object ID="5">
<ObjectClass SecondFeature="vehicle" />
</Object>
</Frame>
You can do it with following XPath expression:
var xml = // your XML string here
var doc = XDocument.Parse(xml);
var frameIds = doc.Root.XPathSelectElements(
"//Frame[./Object/ObjectClass[@SecondFeature ='Vehicle']]")
.Select(n => n.Attribute("ID").Value);
Naturally, if your Frame nodes can be present without ID attributes, you'll need extra null checks in .Select.
Alternatively, non-xpath appraoch (but this is IMHO less readable and calls for even more caution):
var frameIds = doc
.Descendants("ObjectClass")
.Where(n => n.Attribute("SecondFeature").Value == "Vehicle")
.Select(n => n.Parent.Parent.Attribute("ID").Value);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With