Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataContractSerializer vs XmlSerializer: Pros and Cons of each serializer

My desktop application serializes objects using XmlSerializer. I was suggested to leverage DataContractSerializer instead.
Under which scenarios should I use DataContractSerializer?

Many thanks

Comments.
1. The output XML file is stored locally. No other applications deserialize objects from that XML file.
2. My application runs with .NET Framework 3.5 SP1.

like image 578
Vitali Climenco Avatar asked Mar 24 '10 06:03

Vitali Climenco


People also ask

What is DataContractSerializer and how its different from Xmlserializer?

DataContractSerializer can able to serialize types that implements Idictionary whereas XML serializer not. DataContractSerializer serializes all members which are marked with [DataMember] attribute even if member is marked private. XML serializer serialize only public members.

What does XML serializer do?

XML serialization is the process of converting XML data from its representation in the XQuery and XPath data model, which is the hierarchical format it has in a Db2® database, to the serialized string format that it has in an application.


1 Answers

Dan Rigsby has the ultimate post on this - go read it!

XmlSerializer vs. DataContractSerializer (web archive)

He says all there is to say, and in a very convincing way.

In short:

XmlSerializer:

  • has been around for a long time
  • is "opt-out"; everything public gets serialized, unless you tell it not to ([XmlIgnore])

DataContractSerializer is:

  • the new kid in town
  • optimized for speed (about 10% faster than XmlSerializer, typically)
  • "opt-in" - only stuff you specifically mark as [DataMember] will be serialized
  • but anything marked with [DataMember] will be serialized - whether it's public or private
  • doesn't support XML attributes (for speed reasons)
like image 171
marc_s Avatar answered Oct 22 '22 19:10

marc_s