1
私はLINQ to XMLを学んでいましたが、私はちょっと立ち往生したシナリオに遭遇しました。私は、次のXMLがある場合Linq to xml子の属性に基づいてノードのリストを選択
は:
<root>
<planes />
<trains />
<cars>
<car name="civic">
<property name="4doors" />
<property name="4tires" />
</car>
<car name="f150">
<property name="2doors" />
<property name="4tires" />
</car>
<car name="crv">
<property name="4doors" />
<property name="4tires" />
</car>
<car name="scooter">
<property name="2tires" />
</car>
<car name="escape">
<property name="4doors" />
<property name="4tires" />
</car>
</cars>
</root>
をどのように私が4doorsを持っている車のリストを返すことができますか?
これまでのところ、私が試した次の試み:
// This will return a list of nulls
var fourDoorCars = xDoc.Descendants("cars").Descendants("car").Descendants("property").Where(x => x.Attribute("name").Value.Contains("4doors")).Select(x => x.Element("car")).ToList();
// This will return a list of all the 4doors properties.
var fourDoorCars = xDoc.Descendants("cars").Descendants("car").Descendants("property").Where(x => x.Attribute("name").Value.Contains("4doors")).ToList();