.net 性能:XDocument 与 XmlDocument
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4383919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Performance: XDocument versus XmlDocument
提问by CodeMonkey1313
I have read a comparison between the two here. This is primarily a question of performance, relating to both memory and speed.
我在这里阅读了两者之间的比较。这主要是一个性能问题,与内存和速度有关。
I've got several XML documents that are upwards of 100 - 300 K in size. I've noticed that there is some lag when loading this information into an XDocumentrather than an XmlDocumentobject.
我有几个大小超过 100 - 300 K 的 XML 文档。我注意到将这些信息加载到XDocument一个XmlDocument对象而不是一个对象时会有一些滞后。
- Is there a serious performance difference between these two objects?
- Do they access the content of the XML differently?
- When working with a string of XML, which is preferred, or is there a difference?
- 这两个对象之间是否存在严重的性能差异?
- 他们是否以不同的方式访问 XML 的内容?
- 使用 XML 字符串时,哪个是首选,还是有区别?
The end use of these object is to run queries (XPathor LINQ, depending) on the object in question.
这些对象的最终用途是对相关对象运行查询(XPath或 LINQ,具体取决于)。
回答by TheFish
XmlDocument is a purely managed implemenation of the Document Object Model. There is no interop with any COM components, such as the MSXML library. Any claim otherwise is completely bogus. The entire XLinq set of APIs came about as a friendlier way to interact with XML with introduction of LINQ in the .NET Framework.
XmlDocument 是文档对象模型的纯托管实现。没有与任何 COM 组件(例如 MSXML 库)的互操作。否则,任何索赔都是完全虚假的。通过在 .NET Framework 中引入 LINQ,整个 XLinq API 集是作为与 XML 交互的一种更友好的方式出现的。
If you're trying to maximize performance and are comfortable using XPath, try using the XmlDocument and using compiled XPath expressions.
如果您想最大限度地提高性能并且习惯使用 XPath,请尝试使用 XmlDocument 并使用编译的 XPath 表达式。
回答by nawfal
XmlReaderis the lowest API in .NET which all other XML APIs in .NET use under the scenes. Naturally that means it's the hardest to deal with, as well as fastest. It's a streaming API, so it is best fit for memory as well.
XmlReader是 .NET 中最低的 API,.NET 中的所有其他 XML API 在场景下都使用它。自然这意味着它是最难处理的,也是最快的。它是一个流 API,所以它也最适合内存。
Between XmlDocumentand XDocumentaka Linq to XML, here are some raw numbers: http://blogs.msdn.com/b/codejunkie/archive/2008/10/08/xmldocument-vs-xelement-performance.aspx
之间XmlDocument并XDocument又名的LINQ to XML,这里有一些原始数据:http://blogs.msdn.com/b/codejunkie/archive/2008/10/08/xmldocument-vs-xelement-performance.aspx
Both of which find XDocumentclass being faster/more efficient. Programmer productivity/efficiency shouldn't be ignored as well. Personally I find it easier to work with XDocument.
两者都发现XDocument类更快/更有效。程序员的生产力/效率也不应该被忽视。我个人觉得使用XDocument.

