本文介绍了xdocument后代节点与节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我试图理解扩展方法DescendantsnNodes与方法XDocument类的节点"之间的区别.

I'm trying to understand the difference between the extension method, DescendantsnNodes and the method, "Nodes" of the class XDocument.

我可以看到DescendantsNodes应该返回此文档或元素的所有后代并且节点"应返回此文档或元素的所有子节点.

I can see that the DescendantsNodes should return all descendants of this document or elementand Nodes should return all child nodes of this document or element.

我不明白所有子节点"和所有后代"之间有什么区别.

I don't understand what's the difference between "all child nodes" and "all descendants".

有人可以澄清一下吗?

推荐答案

子节点将是直接位于某个特定节点(父节点)下方的节点.后代将是位于同一节点之下"的一个节点,但也可能是子节点之下"的多个级别.

A child node would be a node which is directly "underneath" a certain node (parent). A descendant would be a node which is "underneath" the same node, but it could also be a number of levels "underneath" the child node.

子节点将是子节点,但子节点并不一定是子节点.

A child node will be a descendant, but a descendant won't always be a child node.

Eg: <Parent><Child1 /><Child2><AnotherNode /></Child2></Parent>

-Parent
   |
    --> Child1 (Descendant)
   |
    --> Child2 (Descendant)
          |
           --> AnotherNode (Descendant, not child)

使用较小的代码示例进行可视化可能会更容易:

It might be easier to visualise with a small code sample:

string someXML = "<Root><Child1>Test</Child1><Child2><GrandChild></GrandChild></Child2></Root>";

var xml = XDocument.Parse(someXML);

Console.WriteLine ("XML:");
Console.WriteLine (xml);

Console.WriteLine ("\nNodes();\n");

foreach (XNode node in xml.Descendants("Root").Nodes())
{
    Console.WriteLine ("Child Node:");
    Console.WriteLine (node);
    Console.WriteLine ("");
}

Console.WriteLine ("DescendantNodes();\n");

foreach (XNode node in xml.Descendants("Root").DescendantNodes())
{
    Console.WriteLine ("Descendent Node:");
    Console.WriteLine (node);
    Console.WriteLine ("");
}

产生:

XML:

<Root>
  <Child1>Test</Child1>
  <Child2>
    <GrandChild></GrandChild>
  </Child2>
</Root>

Nodes();

Child Node:
<Child1>Test</Child1>

Child Node:
<Child2>
  <GrandChild></GrandChild>
</Child2>

DescendantNodes();

Descendent Node:
<Child1>Test</Child1>

Descendent Node:
Test

Descendent Node:
<Child2>
  <GrandChild></GrandChild>
</Child2>

Descendent Node:
<GrandChild></GrandChild>

这篇关于xdocument后代节点与节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 13:00