vb.net 如何获得 XElement 的第一个孩子?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18219816/
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
How do I get the first child of an XElement?
提问by Thalecress
The old XmlElement class had a FirstChild property. What is the XElement equivalent?
旧的 XmlElement 类有一个 FirstChild 属性。什么是 XElement 等价物?
Visual Studio rejects .Element(), .Elements()[0]., and .Elements().First()
Visual Studio 拒绝 .Element()、.Elements()[0]. 和 .Elements().First()
回答by Timothy Shields
You want the IEnumerable<XElement> Descendants()method of the XElementclass.
你想要类的IEnumerable<XElement> Descendants()方法XElement。
XElement element = ...;
XElement firstChild = element.Descendants().First();
This sample program:
这个示例程序:
var document = XDocument.Parse(@"
<A x=""some"">
<B y=""data"">
<C/>
</B>
<D/>
</A>
");
Console.WriteLine(document.Root.Descendants().First().ToString());
Produces this output:
产生这个输出:
<B y="data">
<C/>
</B>
回答by Adrian Wragg
http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.aspxstates that XElement has a property FirstNode, inherited from XContainer. This is described as the first child of the current node, and so is probably what you're after.
http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.aspx指出 XElement 有一个属性FirstNode,从XContainer. 这被描述为当前节点的第一个子节点,因此可能是您所追求的。

