Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance: XDocument versus XmlDocument

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.

like image 661
CodeMonkey1313 Avatar asked Dec 08 '10 03:12

CodeMonkey1313


People also ask

What is the difference between XDocument and XmlDocument?

XDocument is from the LINQ to XML API, and XmlDocument is the standard DOM-style API for XML. If you know DOM well, and don't want to learn LINQ to XML, go with XmlDocument . If you're new to both, check out this page that compares the two, and pick which one you like the looks of better.

What is XmlDocument in C#?

The XmlDocument represents an XML document. It can be use to load, modify, validate, an navigate XML documents. The XmlDocument class is an in-memory representation of an XML document. It implements the W3C XML Document Object Model (DOM).


1 Answers

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.

like image 130
TheFish Avatar answered Oct 17 '22 22:10

TheFish