代码之家  ›  专栏  ›  技术社区  ›  Razor

NodesAfterSelf()和ElementsAfterSelf()之间的区别

  •  1
  • Razor  · 技术社区  · 14 年前

    这两个LINQ到XML方法似乎在做同样的事情。想知道两者的区别。

    var xdoc = XDocument.Load(filename);
    
    xdoc.Root.FirstNode.ElementsAfterSelf();
    xdoc.Root.FirstNode.NodesAfterSelf();
    

    <Title Name="Cooking with Computers: Surreptitious Balance Sheets" Price="11.9500">
      <Authors>
        <Author Name="O'Leary, O'Leary" />
        <Author Name="MacFeather, MacFeather" />
      </Authors>
    </Title>  
    <Title Name="You Can Combat Computer Stress!" Price="2.9900">
      <Authors>
        <Author Name="Green, Green" />
      </Authors>
    </Title>  
    

    这是XML

    <PubsDatabase>
      <Title Name="The Busy Executive's Database Guide" Price="19.9900">
        <Authors>
          <Author Name="Green, Green" />
          <Author Name="Bennet, Bennet" />
        </Authors>
      </Title>
      <Title Name="Cooking with Computers: Surreptitious Balance Sheets" Price="11.9500">
        <Authors>
          <Author Name="O'Leary, O'Leary" />
          <Author Name="MacFeather, MacFeather" />
        </Authors>
      </Title>
      <Title Name="You Can Combat Computer Stress!" Price="2.9900">
        <Authors>
          <Author Name="Green, Green" />
        </Authors>
      </Title>
    </PubsDatabase>
    
    1 回复  |  直到 14 年前
        1
  •  1
  •   Jon Skeet    14 年前

    基本上,节点将返回文本节点和元素之类的内容。例如:

    using System;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace Test
    {
        class Test
        {
            static void Main()
            {
                XElement element = new XElement("root",
                    new XElement("child1", "text1"),
                    "text directly in root",
                    new XElement("child2"),
                    new XElement("child3", "text3"));
    
                XElement child1 = element.Element("child1");
                var nodes = child1.NodesAfterSelf();
                foreach (var node in nodes)
                {
                    Console.WriteLine(node.NodeType);
                }
            }
        }
    }
    

    这个指纹

    Text
    Element
    Element
    

    • 不包括节点 在内部 它本身
    • ElementsAfterSelf

    作为旁注,属性 不要

    节点(元素、注释、, 或文本节点)。

    这与大多数xmlapi不同。