3

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>
4

1 に答える 1

1

次のXPath式でそれを行うことができます:

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);

当然、Frameノードが属性なしで存在できる場合IDは、 で追加の null チェックが必要になります.Select

または、非 xpath アプローチ (ただし、これは私見では読みにくく、さらに注意が必要です):

var frameIds = doc
    .Descendants("ObjectClass")
    .Where(n => n.Attribute("SecondFeature").Value == "Vehicle")
    .Select(n => n.Parent.Parent.Attribute("ID").Value);
于 2012-11-04T19:16:56.897 に答える