首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SelectNodes和GetElementsByTagName

SelectNodes和GetElementsByTagName
EN

Stack Overflow用户
提问于 2010-03-25 19:44:47
回答 3查看 8.1K关注 0票数 4

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-03-25 20:14:42

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

getElementByTagName是可在许多语言中使用的DOM Level 1核心标准方法(但在.NET中以大写G拼写)。它只按标记名选择元素;您不能要求它选择具有某个属性的元素,也不能要求它选择其他标记名为b的元素中的标记名为a的元素。它更老、更简单,而且在某些环境中速度更快。

票数 9
EN

Stack Overflow用户

发布于 2010-03-25 20:14:47

SelectNodes接受XPath表达式作为参数,并返回与该表达式匹配的所有节点。

GetElementsByTagName接受标记名作为参数,并返回具有该名称的所有标记。

因此,SelectNodes更具表现力,因为您可以将任何GetElementsByTagName调用编写为SelectNodes调用,但不能反过来编写。XPath是一种非常健壮的表示XML节点集的方式,它提供了比名称更多的过滤方式。例如,XPath可以根据标记名、属性名、内部内容以及标签子标记上的各种聚合函数进行过滤。

票数 3
EN

Stack Overflow用户

发布于 2013-10-09 15:58:41

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

msdn forum上的用户chilberto提供的答案和代码

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

.NET SelectNodes()

代码语言:javascript
复制
    [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()

代码语言:javascript
复制
    [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()和delete node with nodeListi.ParentNode.RemoveChild(nodeListi)就是释放/删除nodeList变量中的引用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2515097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档