I want to find a section of xml based on a criteria so I’m using linq to XML. Unfortunately the result is always null so I guess I’ve done something wrong. Shown below is an example of the XML I’m parsing.
<Stuff>
<ItemsA />
<ItemsB />
<ItemsC>
<Item xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Stuff">
<Id>4bd7b5ac-cb29-4d34-97be-deaebe4a5186</Id>
<Children>
<Item>
<Id>22e3ef6b-4321-40c3-9237-196ba527e9ad </Id>
<Name>SomeName</Name>
</Item>
</Children>
<Description>SomeText</Description>
<Name>NewName</Name>
</Item>
I’m looking at the “ItemsC” section where there may be multiple “Item” blocks of XML (only one shown in this example). I want to retrieve the element based on its “Id”, the one with an Id of “4bd7b5ac-cb29-4d34-97be-deaebe4a5186” in the above example.
The code I have used is shown below:
XElement data = XElement.Parse(GetFile());
var pbp = (from b in data.Descendants("Item")
where b.Element("Id").Value == "4bd7b5ac-cb29-4d34-97be-deaebe4a5186"
select b.Element("Id").Parent).FirstOrDefault();
pbp always returns as null. Can anyone help me to produce the correct linq expression.