It might be a simple fix (well, probably it is), but for some reason I just can't figure it out.
So I have an xml that looks something like this:
XElement xml = XElement.Parse (
@"<Alphabet>
<a name="A" />
<b name="B" />
<d name="D" />
<e name="E" />
</Alphabet>");
So, in my code, I refer to node, which may or may not exist there, for example:
var name = (from b in xml.Descendants("c")
select b.Attribute("name")).FirstOrDefault().Value;
But when it does not exist, instead of returning null or "", it throws a NullReferenceException: Object does not refer to an instance of the object.
What is the best way to check and see if node exists in my linq query? Or do I need to check if it exists in another way?
source
share