Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to XML: handling nodes that do not exist?

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?

like image 441
Paul Mignard Avatar asked Dec 23 '22 07:12

Paul Mignard


2 Answers

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?

like image 68
Jon Skeet Avatar answered Jan 01 '23 20:01

Jon Skeet


I created extension methods to do that for me.

public static string GetAttributeValue(this XElement element, string attributeName)
{
    XAttribute attribute = element.Attribute(attributeName);
    return attribute != null ? attribute.Value : string.Empty;
}

public static string GetElementValue(this XElement element)
{
    return element != null ? element.Value : string.Empty;
}

public static string GetElementValue(this XElement element, string elementName)
{
    XElement child = element.Element(elementName);
    return child != null ? child.Value : string.Empty;
}
like image 32
mhenrixon Avatar answered Jan 01 '23 19:01

mhenrixon