Perhaps I am going about this wrong, but I have an XElement storing an XML object like this
<call>
<attempted/>
<completed>
<welcomecall/>
</completed>
</call>
This is stored inside a call class object - call.callinformation
with data type XElement.
How do I parse the property contents (the xml above) and see if completed contains the child node welcomecall (as not every one will)?
I have tried call.callinformation.Element("completed").Element("welcomecall"
), and other variations, but no luck.
I am trying to assign it to a boolean, like hasWelcomeCall = (code to determine if completed has this child element)
If you don't mind whether or not it's within a completed
element, you could just use:
var hasWelcomeCall = call.Descendants("welcomecall").Any();
If it can be within any one of a number of completed
elements, but you want to check that it's in completed
rather than attempted
, you could use:
var hasWelcomeCall = call.Elements("completed").Elements("welcomecall").Any();
If you're using C# 6 and there's only ever one completed
element you could use:
var hasWelcomeCall = call.Element("completed")?.Element("welcomecall") != null;
See more on this question at Stackoverflow