Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance: XmlSerializer vs XmlReader vs XmlDocument vs XDocument

I'm working on a little web project and would like to read/write to an XML file. Performance is my first priority.

I've come to this great post on comparing the mentioned approaches except XmlSerializer.

I prefer XmlSerializer since it makes the code much cleaner. But I don't know about its performance. What kind does XmlSerializer use inside to write to XML files?

like image 940
Kamyar Avatar asked Dec 21 '10 15:12

Kamyar


People also ask

Is XDocument faster than XmlDocument?

It seems that the XDocument exceeds the XmlDocument quite substantially.

What's the difference between XmlDocument and XmlReader?

Based on my understanding of the two classes, XmlReader should perform faster in my scenario because it reads through an XML document only once, never storing more than the current node in memory. On the contrary, XmlDocument stores the whole XML file in memory which has some performance overhead.


1 Answers

As for the performance of XmlSerializer, see http://msdn.microsoft.com/en-us/library/182eeyhh.aspx which says:

The XmlSerializer creates C# files and compiles them into .dll files to perform this serialization. In .NET Framework 2.0, the XML Serializer Generator Tool (Sgen.exe) is designed to generate these serialization assemblies in advance to be deployed with your application and improve startup performance.

So you can increase performance of XmlSerializer by making use of the sgen tool http://msdn.microsoft.com/en-us/library/bk3w6240.aspx, that way you can avoid the performance hit you get when new XmlSerializer() creates and compiles C# files.

like image 131
Martin Honnen Avatar answered Oct 27 '22 01:10

Martin Honnen