LINQ to XML: handling nodes that do not exist?
c#, linq, linq-to-xml, xml
Solution
Well, you're selecting the attribute - so just use:
var nameAttribute = xml.Descendants("c").Select(b => b.Attribute("name"))
.FirstOrDefault();
if (nameAttribute != null)
{
string name = nameAttribute.Value;
}
else
{
// Whatever...
}
(I've changed it from a query expression to dot notation because the query was trivial - query expression syntax wasn't actually buying you anything.)
One problem with this solution: it doesn't differentiate between there being a "c" element but it not having a "name" attribute, and there not being a "c" element in the first place. Do you need to be able to tell the difference?
Problem
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?