Performance: XDocument versus XmlDocument
Asked Answered
K

3

21

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 XDocument rather than an XmlDocument object.

  • 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?

The end use of these object is to run queries (XPath or LINQ, depending) on the object in question.

Karmakarmadharaya answered 8/12, 2010 at 3:33 Comment(0)
P
21

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.

If you're trying to maximize performance and are comfortable using XPath, try using the XmlDocument and using compiled XPath expressions.

Penza answered 29/12, 2010 at 23:18 Comment(1)
And LINQ doesn't magically improve performance. Nor is that the goal of LINQ - the goal is to increase programmer productivity, which it may if you already know LINQ and you don't know the "old" XML APIs the framework offers.Cavalla
G
8

XmlReader is 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.

Between XmlDocument and XDocument aka Linq to XML, here are some raw numbers: https://learn.microsoft.com/en-us/archive/blogs/codejunkie/xmldocument-vs-xelement-performance

which finds XDocument class being faster/more efficient. Programmer productivity/efficiency shouldn't be ignored as well. Personally I find it easier to work with XDocument.

Glorianna answered 19/8, 2015 at 8:24 Comment(0)
A
2

If anyone else is still looking for the answer... I have managed to do some benchmarking by myself. It seems that the XDocument exceeds the XmlDocument quite substantially. Of course, you could be tempted to involve the XmlReader in this, but this is a topic for another time.

Here's my tiny benchmark result: https://github.com/zulimazuli/dotnetXmlBenchmarks

Airt answered 10/1, 2021 at 21:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.