I am trying to select a xml value by matching on the value of the id tag before it
<unit>
<id>1</id>
<name>foo</name>
</unit>
<unit>
<id>2</id>
<name>bar</name>
</unit>
Given my id is 2 in the above scenario I would like to get the value ‘bar’.
Any advice or library suggestions to do this is appreciated.
2
Answers
I would use LINQ to XML for this:
Parent
property to get to theunit
Element
method to get thename
Sample code:
You can do this in a simple one liner using Linq-to-XML, after loading your file into
XDocument
.