节点()与DescendantNodes()的用法? [英] Nodes() vs DescendantNodes() usages?

查看:668
本文介绍了节点()与DescendantNodes()的用法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读节点()发出的所有节点,包括子。

I read that Nodes() emits all the nodes including sub.

DescendantNodes ()相同,但在递归的方式。

and DescendantNodes() the same but in a recursive way.

但是 - 我不能找到任何情况中,我将需要递归的方式......

however - I cant find any situation in which i will need the recursive way...

当我应该preFER与 DescendantNodes 工作()在节点()?

When should I prefer working with DescendantNodes() over Nodes() ?

即:

 IEnumerable<XNode> nodes =from nd in xmlTree.DescendantNodes()
                select nd;
            foreach (XNode node in nodes)
                Console.WriteLine(node);

输出:

问题:

question :

为什么我需要它递归地分裂,我什么时候才能与节点工作()?

Why will i need it recursively splitted ,when I can work with Nodes() ?

推荐答案

好了的节点给你,你把它当上的 descendantnodes 给你你把它叫做节点的子节点。

Well nodes gives you the child nodes of the node you call it on while descendantnodes gives you the descendant nodes of the node you call it on.

假设你要处理与几个层次嵌套的XML文档,你想找到在各级所有注释节点,那么你可以做

Imagine you have an XML document you want to process with several levels of nesting and you want to find all comment nodes at all levels, then you can do

XDocument doc = XDocument.Parse(@"<!-- comment 1 -->
<root>
<!-- comment 2 -->
  <foo>
    <!-- comment 3 -->
    <bar><!-- comment 4 --></bar>
  </foo>
</root>
<!-- comment 5 -->");

    foreach (XComment comment in doc.DescendantNodes().OfType<XComment>())
    {
        Console.WriteLine(comment.Value);
    }

如果你只使用了节点方法,你需要编写一个递归方法来查找所有注释节点。

If you solely used the Nodes method you would need to write a recursive method to find all comment nodes.

这篇关于节点()与DescendantNodes()的用法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆