XElement kids

How to get only child elements of XElement?

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

I would love IEnumerable just for kids.

+51
c # xml linq-to-xml xelement
Jan 28 '09 at 8:42
source share
3 answers

The immediate children of a single XElement are available by calling Element() or Elements() . Use overloads with a name to access certain elements or without access to all child elements.

There are also similar methods, such as Attribute() and Attributes() , which may be useful to you.

+83
Jan 28 '09 at 10:00
source share
— -

XElement.Nodes () should provide you with what you want.

If you just need XElement child nodes, you may need to limit it (depending on your XML):

 XElement.Nodes().OfType<XElement>() 
+9
Jan 28 '09 at 8:46
source share
+4
Jan 28 '09 at 8:46
source share



All Articles