I have been learning LINQ to XML however I ran into a senario where I'm a little stuck.
If I have the following 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>
How can I return a list of cars who has 4doors?
So far I've tried the following attempts:
// 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();