This may be a simple fix (well, it probably is) but for some reason I just can't figure it out.
So, I have some 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 later in my code, I reference a node that may or may not exist in there like so:
var name = (from b in xml.Descendants("c")
select b.Attribute("name")).FirstOrDefault().Value;
But when it doesn't exist, instead of returning null or "" it throws a NullReferenceException: Object reference not set to an instance of an object.
What's the best way to check and see if a node actually exists in my linq query? Or do I need to check if it exists some other way?