SelectNodes 和 GetElementsByTagName
SelectNodes 和 GetElementsByTagName 之间的主要区别是什么。
what are main differences between SelectNodes and GetElementsByTagName.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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 namea
inside other elements with tag nameb
or anything clever like that. It's older, simpler, and in some environments faster.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 anyGetElementsByTagName
call as aSelectNodes
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() 是 Microsoft 对文档对象模型 (DOM) 的扩展 (msdn)。
Welbog 等人提到的 SelectNodes 采用 XPath 表达式。我想提一下在需要删除 xml 节点时与 GetElementsByTagName() 的区别。
用户 chilberto 在 msdn forum
下一个测试通过执行相同的功能(删除人员节点)但使用 GetElementByTagName() 方法来选择节点来说明差异。尽管返回相同的对象类型,但其构造不同。 SelectNodes() 是对 xml 文档的引用的集合。这意味着我们可以在 foreach 中从文档中删除而不影响引用列表。这通过不受影响的节点列表的计数来表明。 GetElementByTagName() 是一个直接反映文档中节点的集合。这意味着当我们删除父级中的项目时,我们实际上会影响节点的集合。这就是为什么不能在 foreach 中操作节点列表而必须更改为 while 循环的原因。
.NET SelectNodes()
.NET GetElementsByTagName()
通过 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()
.NET GetElementsByTagName()
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.