Export (0) Print
Expand All

How to: Retrieve the Value of an Element (LINQ to XML)

This topic shows how to get the value of elements. There are two main ways to do this. One way is to cast an XElement or an XAttribute to the desired type. The explicit conversion operator then converts the contents of the element or attribute to the specified type and assigns it to your variable. Alternatively, you can use the XElement.Value property or the XAttribute.Value property.

With C#, however, casting is generally the better approach. If you cast the element or attribute to a nullable type, the code is simpler to write when retrieving the value of an element (or attribute) that might or might not exist. The last example in this topic demonstrates this. However, you cannot set the contents of an element through casting, as you can through XElement.Value property.

With Visual Basic, the best approach is to use the XElement.Value property.

To retrieve the value of an element, you just cast the XElement object to your desired type. You can always cast an element to a string, as follows:

XElement e = new XElement("StringElement", "abcde");
Console.WriteLine(e);
Console.WriteLine("Value of e:" + (string)e);

This example produces the following output:

<StringElement>abcde</StringElement>
Value of e:abcde

You can also cast elements to types other than string. For example, if you have an element that contains an integer, you can cast it to int, as shown in the following code:

XElement e = new XElement("Age", "44");
Console.WriteLine(e);
Console.WriteLine("Value of e:" + (int)e);

This example produces the following output:

<Age>44</Age>
Value of e:44

LINQ to XML provides explicit cast operators for the following data types: string, bool, bool?, int, int?, uint, uint?, long, long?, ulong, ulong?, float, float?, double, double?, decimal, decimal?, DateTime, DateTime?, TimeSpan, TimeSpan?, GUID, and GUID?.

LINQ to XML provides the same cast operators for XAttribute objects.

You can use the Value property to retrieve the contents of an element:

XElement e = new XElement("StringElement", "abcde"); 
Console.WriteLine(e);
Console.WriteLine("Value of e:" + e.Value);

This example produces the following output:

<StringElement>abcde</StringElement>
Value of e:abcde

Sometimes you try to retrieve the value of an element even though you are not sure it exists. In this case, when you assign the casted element to a nullable type (either string or one of the nullable types in the .NET Framework), if the element does not exist the assigned variable is just set to null (Nothing in Visual Basic). The following code shows that when the element might or might not exist, it is easier to use casting than to use the Value property.

XElement root = new XElement("Root",
    new XElement("Child1", "child 1 content"),
    new XElement("Child2", "2")
);

// The following assignments show why it is easier to use
// casting when the element might or might not exist.

string c1 = (string)root.Element("Child1");
Console.WriteLine("c1:{0}", c1 == null ? "element does not exist" : c1);

int? c2 = (int?)root.Element("Child2");
Console.WriteLine("c2:{0}", c2 == null ? "element does not exist" : c2.ToString());

string c3 = (string)root.Element("Child3");
Console.WriteLine("c3:{0}", c3 == null ? "element does not exist" : c3);

int? c4 = (int?)root.Element("Child4");
Console.WriteLine("c4:{0}", c4 == null ? "element does not exist" : c4.ToString());

Console.WriteLine();

// The following assignments show the required code when using
// the Value property when the element might or might not exist.
// Notice that this is more difficult than the casting approach.

XElement e1 = root.Element("Child1");
string v1;
if (e1 == null)
    v1 = null;
else
    v1 = e1.Value;
Console.WriteLine("v1:{0}", v1 == null ? "element does not exist" : v1);

XElement e2 = root.Element("Child2");
int? v2;
if (e2 == null)
    v2 = null;
else
    v2 = Int32.Parse(e2.Value);
Console.WriteLine("v2:{0}", v2 == null ? "element does not exist" : v2.ToString());

XElement e3 = root.Element("Child3");
string v3;
if (e3 == null)
    v3 = null;
else
    v3 = e3.Value;
Console.WriteLine("v3:{0}", v3 == null ? "element does not exist" : v3);

XElement e4 = root.Element("Child4");
int? v4;
if (e4 == null)
    v4 = null;
else
    v4 = Int32.Parse(e4.Value);
Console.WriteLine("v4:{0}", v4 == null ? "element does not exist" : v4.ToString());

This code produces the following output:

c1:child 1 content
c2:2
c3:element does not exist
c4:element does not exist

v1:child 1 content
v2:2
v3:element does not exist
v4:element does not exist

In general, you can write simpler code when using casting to retrieve the contents of elements and attributes.

Show:
© 2014 Microsoft