SelectNodes 和 GetElementsByTagName
-
22-09-2019 - |
题
SelectNodes 和 GetElementsByTagName 之间的主要区别是什么。
解决方案
的SelectNodes 是一个.NET /即得到用于的XPath表达式匹配的节点的列表MSXML特定方法。的XPath可以选择由标签名称元素,还可以做很多的其它更复杂的选择规则。
getElementByTagName 是在许多语言中使用(但有一个拼写DOM级别1核心标准方法在.NET资本G
)。它仅选择由标签名的元素;你不能要求它来选择有特定属性的元素,或与标签名称a
或任何类似巧妙的其他元素中的标记名称b
元素。这是旧的,更简单,在某些环境中更快。
其他提示
SelectNodes
需要一个的XPath表达式作为参数并返回所有节点,且符合表达。
GetElementsByTagName
需要一个标签名作为参数,并返回具有该名称的所有标签。
SelectNodes
因此更具有表现力,因为你可以写任何GetElementsByTagName
呼叫作为SelectNodes
电话,但不是周围的其他方式。 XPath是表达套XML节点,提供的不仅仅是名称筛选的多种方式的一个非常可靠的方法。的XPath,例如,可以通过标记名称,属性名称,内容物和标签的儿童不同骨料功能以及进行过滤。
SelectNodes() 是 Microsoft 对文档对象模型 (DOM) 的扩展(msdn)。Welbog 等人提到的 SelectNodes 采用 XPath 表达式。我想提一下在需要删除 xml 节点时与 GetElementsByTagName() 的区别。
下一个测试通过执行相同的功能(删除人员节点)但使用 GetElementByTagName() 方法来选择节点来说明差异。尽管返回相同的对象类型,但其构造不同。SelectNodes() 是对 xml 文档的引用的集合。这意味着我们可以在 foreach 中从文档中删除而不影响引用列表。这通过不受影响的节点列表的计数来表明。GetElementByTagName() 是一个直接反映文档中节点的集合。这意味着当我们删除父级中的项目时,我们实际上会影响节点的集合。这就是为什么不能在 foreach 中操作节点列表而必须更改为 while 循环的原因。
.NET 选择节点()
[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 变量中的引用。