I’m using Linq to parse a simple XML file into a var (ienumerable), I’m stuggling at getting the data out into the seperate string variables. I’m using a simple foreach loop (there is only one result) but it’s not not outputting anything. Here’s the code i’m using;
XElement xmlResults = XElement.Parse(e.Result);
var timerresult = from timers in xmlResults.Descendants("e2simplexmlresult")
select new GetResult
{
e2state = timers.Element("e2state").Value,
e2statetext = timers.Element("e2statetext").Value
};
foreach (var item in timerresult)
{
Debug.WriteLine(item.e2state);
Debug.WriteLine(item.e2statetext);
}
Is there something i’m not doing correctly, is this the best way to achieve the desired result?
EDIT: Here’s the XML;
<e2simplexmlresult>
<e2state>False</e2state>
<e2statetext>Conflicting Timer(s) detected! / A Place in the Sun: Home or Away / Goals On Sunday</e2statetext>
</e2simplexmlresult>
You can change XElement to XDocument, or still use the XElement in combination with DescendantsAndSelf(…). By using XElement, the “e2simplexmlresult” tag is not part of your result , thus has no descendants. Using DescendantsAndSelf() or using XDocument overcomes this issue.