Performance: XDocument versus XmlDocument Performance: XDocument versus XmlDocument xml xml

Performance: XDocument versus XmlDocument


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.


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://docs.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.


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