How to retrieve a single child element (LINQ to XML)

This article explains how to retrieve a single child element, the first child element that has a specified name. In C# you do this with the Element method. In Visual Basic you do it with array indexer notation.

Example: Retrieve the first element that has a specified name

The following example retrieves the first DeliveryNotes element from the XML document in Sample XML file: Typical purchase order.

XElement po = XElement.Load("PurchaseOrder.xml");
XElement e = po.Element("DeliveryNotes");
Console.WriteLine(e);
Dim po As XElement = XElement.Load("PurchaseOrder.xml")
Dim e As XElement = po.<DeliveryNotes>(0)
Console.WriteLine(e)

This example produces the following output:

<DeliveryNotes>Please leave packages in shed by driveway.</DeliveryNotes>

Example: Retrieve from XML that's in a namespace

The following example does the same thing as the one above, but for XML that's in a namespace. It uses the XML document Sample XML file: Typical purchase order in a namespace. For more information about namespaces, see Namespaces overview.

XElement po = XElement.Load("PurchaseOrderInNamespace.xml");
XNamespace aw = "http://www.adventure-works.com";
XElement e = po.Element(aw + "DeliveryNotes");
Console.WriteLine(e);
Imports <xmlns:aw="http://www.adventure-works.com">

Module Module1
    Sub Main()
        Dim po As XElement = XElement.Load("PurchaseOrderInNamespace.xml")
        Dim e As XElement = po.<aw:DeliveryNotes>(0)
        Console.WriteLine(e)
    End Sub
End Module

This example produces the following output:

<aw:DeliveryNotes xmlns:aw="http://www.adventure-works.com">Please leave packages in shed by driveway.</aw:DeliveryNotes>

See also