Children of XElement

2019-02-02 19:23发布

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.

3条回答
▲ chillily
3楼-- · 2019-02-02 20:03

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>()
查看更多
劳资没心,怎么记你
4楼-- · 2019-02-02 20:04

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.

查看更多
登录 后发表回答