SelectNodes 和 GetElementsByTagName

发布于 2024-08-27 06:11:38 字数 53 浏览 5 评论 0原文

SelectNodes 和 GetElementsByTagName 之间的主要区别是什么。

what are main differences between SelectNodes and GetElementsByTagName.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

带刺的爱情 2024-09-03 06:11:38

SelectNodes 是 .NET/MSXML 特定的获取 XPath 表达式的匹配节点列表的方法。 XPath 可以通过标签名称选择元素,但也可以执行许多其他更复杂的选择规则。

getElementByTagName 是多种语言中可用的 DOM Level 1 核心标准方法(但拼写为大写 < .NET 中的代码>G)。它仅通过标签名称选择元素;你不能要求它选择具有特定属性的元素,或者在具有标签名称 b 的其他元素中选择具有标签名称 a 的元素,或者类似的聪明的东西。它更旧、更简单,并且在某些环境中速度更快。

SelectNodes is a .NET/MSXML-specific method that gets a list of matching nodes for an XPath expression. XPaths can select elements by tag name but can also do lots of other, more complicated selection rules.

getElementByTagName is a DOM Level 1 Core standard method available in many languages (but spelled with a capital G in .NET). It selects elements only by tag name; you can't ask it to select elements with a certain attribute, or elements with tag name a inside other elements with tag name b or anything clever like that. It's older, simpler, and in some environments faster.

泛泛之交 2024-09-03 06:11:38

SelectNodes 采用 XPath 表达式作为参数,并返回所有匹配的节点那个表情。

GetElementsByTagName 将标签名称作为参数并返回具有该名称的所有标签。

因此,SelectNodes 更具表现力,因为您可以将任何 GetElementsByTagName 调用编写为 SelectNodes 调用,但反之则不然。 XPath 是表达 XML 节点集的一种非常强大的方式,提供了比名称更多的过滤方式。例如,XPath 还可以按标签名称、属性名称、内部内容以及子标签上的各种聚合函数进行过滤。

SelectNodes takes an XPath expression as a parameter and returns all nodes that match that expression.

GetElementsByTagName takes a tag name as a parameter and returns all tags that have that name.

SelectNodes is therefore more expressive, as you can write any GetElementsByTagName call as a SelectNodes call, but not the other way around. XPath is a very robust way of expressing sets of XML nodes, offering more ways of filtering than just name. XPath, for example, can filter by tag name, attribute names, inner content and various aggregate functions on tag children as well.

浅听莫相离 2024-09-03 06:11:38

SelectNodes() 是 Microsoft 对文档对象模型 (DOM) 的扩展 (msdn)。
Welbog 等人提到的 SelectNodes 采用 XPath 表达式。我想提一下在需要删除 xml 节点时与 GetElementsByTagName() 的区别。

用户 chilbertomsdn forum

下一个测试通过执行相同的功能(删除人员节点)但使用 GetElementByTagName() 方法来选择节点来说明差异。尽管返回相同的对象类型,但其构造不同。 SelectNodes() 是对 xml 文档的引用的集合。这意味着我们可以在 foreach 中从文档中删除而不影响引用列表。这通过不受影响的节点列表的计数来表明。 GetElementByTagName() 是一个直接反映文档中节点的集合。这意味着当我们删除父级中的项目时,我们实际上会影响节点的集合。这就是为什么不能在 foreach 中操作节点列表而必须更改为 while 循环的原因。

.NET SelectNodes()

    [TestMethod]
    public void TestSelectNodesBehavior()
    {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(@"<root>
                               <person>
                                 <id>1</id>
                                 <name>j</name>
                                </person>
                                <person>
                                  <id>2</id>
                                  <name>j</name>
                                </person>
                                <person>
                                  <id>1</id>
                                  <name>j</name>
                                 </person>
                                 <person>
                                   <id>3</id>
                                   <name>j</name>
                                  </person>
                                  <business></business>
                                </root>");

        XmlNodeList nodeList = doc.SelectNodes("/root/person");

        Assert.AreEqual(5, doc.FirstChild.ChildNodes.Count, "There should have been a total of 5 nodes: 4 person nodes and 1 business node");
        Assert.AreEqual(4, nodeList.Count, "There should have been a total of 4 nodes");

        foreach (XmlNode n in nodeList)
            n.ParentNode.RemoveChild(n);

        Assert.AreEqual(1, doc.FirstChild.ChildNodes.Count, "There should have been only 1 business node left in the document");
        Assert.AreEqual(4, nodeList.Count, "There should have been a total of 4 nodes");
    }

.NET GetElementsByTagName()

    [TestMethod]
    public void TestGetElementsByTagNameBehavior()
    {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(@"<root>
                               <person>
                                 <id>1</id>
                                 <name>j</name>
                                </person>
                                <person>
                                  <id>2</id>
                                  <name>j</name>
                                </person>
                                <person>
                                  <id>1</id>
                                  <name>j</name>
                                 </person>
                                 <person>
                                   <id>3</id>
                                   <name>j</name>
                                  </person>
                                  <business></business>
                                </root>");;

        XmlNodeList nodeList = doc.GetElementsByTagName("person");

        Assert.AreEqual(5, doc.FirstChild.ChildNodes.Count, "There should have been a total of 5 nodes: 4 person nodes and 1 business node");
        Assert.AreEqual(4, nodeList.Count, "There should have been a total of 4 nodes");

        while (nodeList.Count > 0)
            nodeList[0].ParentNode.RemoveChild(nodeList[0]);

        Assert.AreEqual(1, doc.FirstChild.ChildNodes.Count, "There should have been only 1 business node left in the document");
        Assert.AreEqual(0, nodeList.Count, "All the nodes have been removed");
    }

通过 SelectNodes(),我们可以获得 xml 文档节点引用的集合/列表。我们可以利用这些引用进行操作。如果我们删除节点,更改将对 xml 文档可见,但引用的集合/列表是相同的(虽然节点被删除,但它的引用现在指向 null -> System.NullReferenceException)虽然我真的不知道这是如何实施的。我想如果我们使用 XmlNodeList nodeList = GetElementsByTagName() 并使用 nodeList[i].ParentNode.RemoveChild(nodeList[i]) 删除节点,则会释放/删除 nodeList 变量中的引用。

SelectNodes() is a Microsoft extension to the Document Object Model (DOM) (msdn).
SelectNodes as mentioned by Welbog and others takes XPath expression. I would like to mention difference with GetElementsByTagName() when deleting xml node is needed.

Answer and code provided user chilberto at msdn forum

The next test illustrates the difference by performing the same function (removing the person nodes) but by using the GetElementByTagName() method to select the nodes. Though the same object type is returned its construction is different. The SelectNodes() is a collection of references back to the xml document. That means we can remove from the document in a foreach without affecting the list of references. This is shown by the count of the nodelist not being affected. The GetElementByTagName() is a collection that directly reflects the nodes in the document. That means as we remove the items in the parent, we actually affect the collection of nodes. This is why the nodelist can not be manipulated in a foreach but had to be changed to a while loop.

.NET SelectNodes()

    [TestMethod]
    public void TestSelectNodesBehavior()
    {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(@"<root>
                               <person>
                                 <id>1</id>
                                 <name>j</name>
                                </person>
                                <person>
                                  <id>2</id>
                                  <name>j</name>
                                </person>
                                <person>
                                  <id>1</id>
                                  <name>j</name>
                                 </person>
                                 <person>
                                   <id>3</id>
                                   <name>j</name>
                                  </person>
                                  <business></business>
                                </root>");

        XmlNodeList nodeList = doc.SelectNodes("/root/person");

        Assert.AreEqual(5, doc.FirstChild.ChildNodes.Count, "There should have been a total of 5 nodes: 4 person nodes and 1 business node");
        Assert.AreEqual(4, nodeList.Count, "There should have been a total of 4 nodes");

        foreach (XmlNode n in nodeList)
            n.ParentNode.RemoveChild(n);

        Assert.AreEqual(1, doc.FirstChild.ChildNodes.Count, "There should have been only 1 business node left in the document");
        Assert.AreEqual(4, nodeList.Count, "There should have been a total of 4 nodes");
    }

.NET GetElementsByTagName()

    [TestMethod]
    public void TestGetElementsByTagNameBehavior()
    {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(@"<root>
                               <person>
                                 <id>1</id>
                                 <name>j</name>
                                </person>
                                <person>
                                  <id>2</id>
                                  <name>j</name>
                                </person>
                                <person>
                                  <id>1</id>
                                  <name>j</name>
                                 </person>
                                 <person>
                                   <id>3</id>
                                   <name>j</name>
                                  </person>
                                  <business></business>
                                </root>");;

        XmlNodeList nodeList = doc.GetElementsByTagName("person");

        Assert.AreEqual(5, doc.FirstChild.ChildNodes.Count, "There should have been a total of 5 nodes: 4 person nodes and 1 business node");
        Assert.AreEqual(4, nodeList.Count, "There should have been a total of 4 nodes");

        while (nodeList.Count > 0)
            nodeList[0].ParentNode.RemoveChild(nodeList[0]);

        Assert.AreEqual(1, doc.FirstChild.ChildNodes.Count, "There should have been only 1 business node left in the document");
        Assert.AreEqual(0, nodeList.Count, "All the nodes have been removed");
    }

With SelectNodes() we get collection / list of references to xml document nodes. We can manipulate with those references. If we delete node, the change will be visible to xml document, but the collection / list of references is the same (although node which was deleted, it's reference points now to null -> System.NullReferenceException) Although I do not really know how this is implemented. I suppose if we use XmlNodeList nodeList = GetElementsByTagName() and delete node with nodeList[i].ParentNode.RemoveChild(nodeList[i]) is frees/deletes reference in nodeList variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文