哪种性能最好:使用 XPath 的 XPathNavigator 与使用查询的 Linq to Xml?
我有一个应用程序,在其中使用 XPathNavigator 来迭代节点。它运行良好。
但我想知道,如果我使用 LINQ to Xml...
我会得到什么好处(性能、可维护性)?
使用 XPath、LINQ to Xml 对性能有何影响?
我正在使用 C#.net、VS 2010,我的 .xml 是中等大小。
I have an application in which I am using XPathNavigator to iterate nodes. It is working fine.
But I want to know that if I use LINQ to Xml....
What benefits(Performance, maintainability) I will get?
With XPath, LINQ to Xml what is the performance hit?
I am using C#.net, VS 2010 and my .xml is mid size.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只是补充一下这里已经说过的内容,整体性能似乎取决于您对相关文档的实际操作。这是我根据比较 XElement 和 XPathNavigator 之间的解析性能的简单实验运行得出的结论。
如果您正在选择节点,遍历这些节点并读取一些属性值:
近似系数 1.5。
比 XPathNavigator.Select 大约为 0.5。
大约为 0.5 的系数。
另一方面,如果您还读取每个节点子节点的值,就会有点有趣:
这些结论基于以下代码:
使用从 这里
总体而言,看起来 XElement 解析 API(不包括 XPath 扩展)可以为您提供最佳性能,同时如果您的文档有点 平坦的。如果您有深层嵌套结构,您必须执行类似的操作
,那么 XElement.XPathSelectElement 可能会提供性能和代码可维护性之间的最佳折衷方案。
Just to add onto what has already been stated here, overall performance seems to depends on what you are actually doing with the document in question. This is what I have concluded based on a simple experimental run comparing parsing performance between XElement to XPathNavigator.
If you are selecting nodes, traversing these nodes and reading some attribute values:
approximate factor of 1.5.
than XPathNavigator.Select by an approximate factor of 0.5.
an approximate factor of 0.5.
On the other hand, if you are also reading the value of each node's children it gets a little interesting:
These conclusions are based on the following code:
with books.xml downloaded from here
Overall, it looks like the XElement parsing API, excluding the XPath extensions, gives you the best performance, while also easier to use, if your document is somewhat flat. If you have deep nested structures where you have to do something like
then XElement.XPathSelectElement may provide the best compromise between performance and code maintainability.
嗯,
XPathNavigator
通常比Linq to XML
查询更快。但总有“但是”。Linq to XML
肯定会让您的代码更具可读性和可维护性。阅读 linq 查询然后分析 XPath 更容易(至少对我来说)。另外 - 在编写查询时您将获得智能感知,这将有助于使您的代码正确。Linq to XML
还使您可以轻松修改数据(如果您需要的话)。XPathNavigator
为您提供只读访问权限。另一方面,如果您确实需要顶级性能,
XPathNavigator
可能是最佳选择。这仅取决于您当前的情况以及您想要实现的目标。如果性能不是问题(XML 文件相当小,您不会对此文件发出很多请求等等),您可以轻松使用Linq to XML
。否则请密切关注XPathNavigator
。Well,
XPathNavigator
will generally be faster thanLinq to XML
queries. But there's always 'but'.Linq to XML
will definitely make your code more readable and maintainable. It's easier (at least for me) to read linq query then analyze XPath. Also - you will get intellisense when writing query which will help to make your code correct.Linq to XML
also gives you possibility to easily modify data, if that's what you need.XPathNavigator
gives you readonly access.On the other hand, if you really need top performance,
XPathNavigator
is probably the way to go. It simply depends on your current scenario and what you're trying to accomplish. If performance is not an issue (XML file is rather small, you won't make many requests to this file and so on) you can easily go withLinq to XML
. Otherwise stick close toXPathNavigator
.