How do I get the first child of an XElement?

You want the IEnumerable<XElement> Descendants() method of the XElement class.

XElement element = ...;
XElement firstChild = element.Descendants().First();

This sample program:

var document = XDocument.Parse(@"
    <A x=""some"">
        <B y=""data"">
            <C/>
        </B>
        <D/>
    </A>
    ");

Console.WriteLine(document.Root.Descendants().First().ToString());

Produces this output:

<B y="data">
    <C/>
</B>

http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.aspx states that XElement has a property FirstNode, inherited from XContainer. This is described as the first child of the current node, and so is probably what you're after.

Tags:

C#

.Net

Vb.Net