Children of XElement

Superman picture Superman · Jan 28, 2009 · Viewed 51.9k times · Source

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.

Answer

Bevan picture Bevan · Jan 28, 2009

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.