C# XElement 的孩子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/486912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 05:19:27  来源:igfitidea点击:

Children of XElement

c#xmllinq-to-xmlxelement

提问by Superman

How do I get just the children of an XElement?

我如何只获得 XElement 的孩子?

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

我目前正在使用 XElement.Descendants() 函数,它返回所有级别的 XElements,而不仅仅是子节点。

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

我真正想要的是只有孩子的 IEnumerable。

采纳答案by Bevan

The immediate child elements of one XElementare 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.

可以通过调用或函数访问一个XElement的直接子元素。使用带名称的重载访问特定元素,或不访问所有子元素。Element()Elements()

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

也有像类似的方法Attribute(),并Attributes()可能会对您有用。

回答by tvanfosson

回答by Steven Robbins

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

XElement.Nodes() 应该可以满足您的需求。

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

如果您只想要 XElement 子节点,那么您可能需要通过以下方式限制它(取决于您的 XML):

XElement.Nodes().OfType<XElement>()