Linq To XML: Get Child XElement From Parent With Unknown Name
I currently have some XML similar to the following
<First Node>
<Second Node>
<Third Node>
<Fourth Node>
'Lots of Children in here
</Fourth Node>
</Third Node>
</Second Node>
</First Node>
The issue is I know the exact names of the First, Second and Fourth Nodes. However the Third Node can be from a very wide range of possible names. I have no way to change the struct开发者_StackOverflow中文版ure of the XML when it's being created(it's coming from a third party interface).
I've attempted some linq similar to the following
tempElement = (From secondElement In xmlDoc.Descendants Select XMLDoc.Element("First Node").Element("Second Node")).FirstOrDefault
I'm using that to trim down unnecessary XML. So after that I have a XElement with
<Second Node>
<Third Node>
<Fourth Node>
</Fourth Node>
</Third Node>
</Second Node>
Then on that I'm using this linq statement to try to return just the Fourth Node
fourthElement = tempElement.ElementsAfterSelf.FirstOrDefault
This is returning me a null value. What would be the correct linq there, or is there a better way to do this?
Thanks
Just use Elements()
instead of ElementsAfterSelf()
- currently it's looking for elements after SecondNode instead of elements within SecondNode.
精彩评论