Difference Between DataContractSerializer and XMLSerializer

Kailash Chandra Behera | Monday, May 23, 2016

Introduction

Serialization is the process of converting an object into streams, bytes, or into memory, basically into a state so that it can recreate into an object when it is necessary. There are many serializer available int he software world, here his blog describes main differences available between DataContractSerializer and XMLSerializer.

Getting Started

Both DataContractSerializer and XMLSerializer are used to serialize object into XML format but they have some differences, here i have discussed some measure differences exists between DataContractSerializer and XMLSerializer.

Performance

DataContractSerializer gives better performance over Xmlserializer. This is because DataContratSerializer explicitly shows the which fields or properties are serialized into XML. Where as XMLSerializer does not provide better performance, because XMLSerializer does not indicate which fields or properties of the type are serialized into XML.

Capability

The DataContractSerializer can translate the HashTable,Dictionary, Private and public member into XML. Where as XMLSerializer cannot translate the HashTable,Dictionary and private member into XML only public member translates into XML.

When To Use

XmlSerializer gives more control over the generated xml structure compared to the DataContractSerializer.For ex, if a field should come as an attribute or element. Hence in the case of implementation complex schema XMLSerializer can be used, Where as DataContractSerializer does not give more control over the generated xml structure compared to the XmlSerializer, hence DataContractSerializer is basically for very small and simple subset of the XML infoset.

Who uses

The default serializer of WCF Service is DataContractSerializer, hence WCF Service is mostly uses this DataContractSerializer. Where as the default serializer of WebService is Xmlserializer, hence Webservice mostly uses the Xmlserializer.

Constructor

DataContractSerializer does not need default constructor of the class whose object is to be serialize, where as XmlSerializer need the default constructor of the class whose object is to be serialize.

Approch

DataContractSerializer uses the opts-in approach i.e, selecting the members that need to be serialized. This is called an opt-in approach. Whereas XmlSerializer uses an opt-out approach i.e., marking the members do not need to be serialized. This is called an opt-out approach.

Summary

In the above discussion, we have discussed the measure differences exist between DataContractSerializer and XmlSerializer, Hope you have got the idea of the details about the differences between DataContractSerializer and XmlSerializer.

Thanks