Linq to XML 后代和元素之间有什么区别
我在 VS IntelliSense 中遇到过这两个关键字。我试图用谷歌搜索它们之间的区别,但没有得到明确的答案。其中哪一种对于中小型 XML 文件具有最佳性能。谢谢
I have came across both these keywords in the VS IntelliSense. I tried to googling the difference between them and did not get a clear answer. Which one of these have the best performance with small to medium XML files. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Elements
仅查找那些 <直系后代,即直系子女。后代
查找任何级别的子级,即子元素、孙子元素等...下面是一个演示差异的示例:
代码:
结果:
如果您知道所需的元素是直接子元素,那么如果您使用
Elements
,您将获得更好的性能而不是后代
。Elements
finds only those elements that are direct descendents, i.e. immediate children.Descendants
finds children at any level, i.e. children, grand-children, etc...Here is an example demonstrating the difference:
Code:
Result:
If you know that the elements you want are immediate children then you will get better performance if you use
Elements
instead ofDescendants
.后代将在当前元素的整个子树中搜索指定名称(如果未提供名称,则返回树的扁平版本),而元素仅搜索当前元素的直接子元素。
Descendants
will search the entire subtree of the current element for the specified name (or will return a flattened version of the tree if no name is provided), whereasElements
searches only the immediate children of the current element.