The old XmlElement class had a FirstChild property. What is the XElement equivalent?
Visual Studio rejects .Element(), .Elements()[0]., and .Elements().First()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With