I have an xml message that I need to parse (No control of format) that looks something like the below (Name / Value Pairs) that I need to process. What would be the best method for querying the values out where Name = x and Get the related Value?
I am currently using a nested select to try and get the value out of a specific Name / Value pair. Just wondering if an easier LINQ / Lambda call I could use instead.
Any suggestions would be appreciated.
<Message>
<MessageContent>
<MessageDetails>
<Name>System_ID</Name>
<Value>12345</Value>
</MessageDetails>
<MessageDetails>
<Name>System_Name</Name>
<Value>Test System</Value>
</MessageDetails>
</MessageContent>
</Message>
Use Linq to XML:
If you expect only one match add a
FirstOrDefault()to get the first matching value.Judging by your xml it looks like you could also benefit from projecting to a dictionary (if your
Nameelement values are unique):Now you can just use the dictionary: