I have this XML file:
<MyFile>
<PossibleResponses>
<Response text="response1"/>
<Response text="response2"/>
</PossibleResponses>
</MyFile>
I’m trying to retrieve one of the responses:
var selectedResponse = (from i in XElement.Load("MyFile.xml").Elements("PossibleResponses")
where i.Element("Response").Attribute("text").Value == parameter
select i.Element("Response")).Single();
Now if I’m trying to get the first response (parameter = response1) then it works.
If I’m trying to get the second response (parameter = response2) then it returns no result.
If I switch the order in the XML file them then response2 is returned but response 1 isn’t.
I was helped by this response but made some amendments:
According to my limited understanding of Linq, I think the key here is that ‘XElement.Load(“MyFile.xml”).Descendants(“Response”)’ returns a list of elements so:
1. ‘Where’ clause should refer directly to the element level, no need to ‘re-mention’ its an element.
2. The same for the ‘Select’ clause.
Any other clarifications would be welcome 🙂