Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between DataContractSerializer vs XmlSerializer

Tags:

wcf

I was going through WCF Fundamentals, Can anybody tells that under which scenarios should we use DataContractSerializer and XmlSerializer?

like image 706
Arslan Bhatti Avatar asked Sep 09 '14 05:09

Arslan Bhatti


People also ask

Why do we use XmlSerializer class?

XmlSerializer enables you to control how objects are encoded into XML. The XmlSerializer enables you to control how objects are encoded into XML, it has a number of constructors.

Is XmlSerializer thread safe?

Since XmlSerializer is one of the few thread safe classes in the framework you really only need a single instance of each serializer even in a multithreaded application.

Which namespace is used for serialization in WCF?

By default WCF uses the DataContractSerializer class to serialize data types.

What is by serialization with respect to WCF?

The process forms a sequence of bytes into a logical object; this is called an encoding process. At runtime when WCF receives the logical message, it transforms them back into corresponding . Net objects. This process is called serialization.


2 Answers

DataContractSerializer

  • Is meant to be used for serialization/deserialization of class in WCF service to and from either JSON or XML.
  • serializes properties and fields.
  • Is faster than XmlSerializer
  • Doesn't control how xml is generated. Should not be used when full control on generated XML structure is required

XMLSerializer

  • XmlSerializer is only for XML serialization
  • Supports full control over the XML structure
  • Serializes only public properties
like image 175
Ronak Patel Avatar answered Oct 22 '22 01:10

Ronak Patel


  1. DataContractSerializer is better performance over Xmlserializer. This is because DataContratSerializer explicitly shows the which fields or properties are serialized into XML.

  2. DataContractSerializer can able to serialize types that implements Idictionary whereas XML serializer not.

  3. DataContractSerializer serializes all members which are marked with [DataMember] attribute even if member is marked private. XML serializer serialize only public members.

These are some important difference.

like image 24
Vikas Gupta Avatar answered Oct 22 '22 01:10

Vikas Gupta