开发者

SelectNodes and GetElementsByTagName

开发者 https://www.devze.com 2022-12-25 02:35 出处:网络
what are main differences between SelectNodes and GetElementsBy开发者_如何学运维TagName.SelectNodes is a .NET/MSXML-specific method that gets a list of matching nodes for an XPath expression. XPaths c

what are main differences between SelectNodes and GetElementsBy开发者_如何学运维TagName.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消