GowthamanSS
GowthamanSS

Reputation: 1474

Getting null elements using xelement with c# .net

Let us consider the following xml document:

<contact>
    <name>Hines</name>
    <phone>206-555-0144</phone>
    <mobile>425-555-0145</mobile>
</contact>

from which I retrieve a value as

var value = parent.Element("name").Value;

The code above will throw a NullReferenceException if "name" is not present, as Element will return null in C# but not in vb.net which will throw empty value.

So my problem is to identify when the xml nodes below the root node are missing and to get an empty value instead.

Upvotes: 0

Views: 5887

Answers (3)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112334

You could create an extension method that can easily be reused. Place it in a static class

public static string ElementValue(this XElement parent, string elementName)
{
    var xel = parent.Element(elementName);
    return xel == null ? "" : xel.Value;
}

Now you can call it like this

string result = parent.ElementValue("name");

UPDATE

If you return null instead of an empty string when the element is not present, this gives you the possibility to differentiate between an empty element and the absence of an element.

public static string ElementValue(this XElement parent, string elementName)
{
    var xel = parent.Element(elementName);
    return xel == null ? null : xel.Value;
}

 

string result = parent.ElementValue("name");
if (result == null) {
    Console.WriteLine("Element 'name' is missing!");
} else {
    Console.WriteLine("Name = {0}", result);
}

EDIT

Microsoft uses the following pattern in different places in the .NET Framework Class Library

public static bool TryGetValue(this XElement parent, string elementName,
                                                     out string value)
{
    var xel = parent.Element(elementName);
    if (xel == null) {
        value = null;
        return false;
    }
    value = xel.Value;
    return true;
}

It can be called like this

string result;
if (parent.TryGetValue("name", out result)) {
    Console.WriteLine("Name = {0}", result);
}

UPDATE

With C# 6.0 (Visual Studio 2015) Microsoft has introduced the null propagation operator ?. simplifying things a lot:

var value = parent.Element("name")?.Value;

This will simply set the value to null even if the element was not found.

You can also combine it with the coalesce operator ??, if you want to return another value than null:

var value = parent.Element("name")?.Value ?? "";

Upvotes: 6

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236208

Simply cast your element to some nullable type. XElement has a bunch of overloaded explicit casting operators, which will cast elements value to required type:

string value = (string)parent.Element("name");

In this case if element <name> will not be found, you will get string with value equal to null. NullReferenceException will not be raised.

I think if element not exist in xml, then null is the only appropriate value for that element. But if you really need to have empty string instead, then:

string value = (string)parent.Element("name") ?? "";

Upvotes: 4

Danilo Vulović
Danilo Vulović

Reputation: 3063

var value = parent.Element("name") != null ? parent.Element("name").Value : ""

Upvotes: 0

Related Questions