Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Children of XElement

How do I get just the children of an XElement?

I am currently using the XElement.Descendants() function, which returns all levels of XElements, rather than just the child nodes.

What I would really like is an IEnumerable of just the children.

like image 964
Superman Avatar asked Jan 28 '09 08:01

Superman


2 Answers

The immediate child elements of one XElement are accessible by calling the Element() or Elements() functions. Use the overloads with a name to access specific elements, or without to access all child elements.

There are also similar methods like Attribute() and Attributes() that you might find useful.

like image 56
Bevan Avatar answered Nov 20 '22 01:11

Bevan


XElement.Nodes() should get you what you want.

If you just want the XElement child nodes then you might need to restrict it (depending on your XML) with:

XElement.Nodes().OfType<XElement>()
like image 13
Steven Robbins Avatar answered Nov 20 '22 01:11

Steven Robbins