Arslan Bhatti
Arslan Bhatti

Reputation: 193

Difference between DataContractSerializer vs XmlSerializer

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

Upvotes: 13

Views: 16116

Answers (2)

Ronak Patel
Ronak Patel

Reputation: 2610

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

Upvotes: 13

Vikas Gupta
Vikas Gupta

Reputation: 188

  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.

Upvotes: 12

Related Questions