Difference between DataContractSerializer and XMLSerializer


DataContractSerializer

1. WCF uses DataContractSerializer attribute.

2. A practical benefit of the design of the DataContractSerializer is better performance over Xmlserializer. This is because DataContratSerializer explicitly shows the which fields or properties are serialized into XML. DataContractSerializer uses the opts-in approach i.e, selecting the members that needs to be serialized.

3. The DataContractSerializer can translate the HashTable into XML.

4. DataContractSerializer serializes private members.

5. DataContractSerializer can serialize both private and public types.

6. DataContractSerializer does not need any default constructor before serializing any type.

7. DataContractSerializer does not give more control over the generated xml structure compared to the XmlSerializer.

8. DataContractSerializer can able to serialize types that implements Idictionary.

XMLSerializer

1. Webservice uses XMLSerializer attribute

2. XMLSerializer does not provide better performance when compare with DataContratSerializer because XMLSerializer does not indicate which fields or properties of the type are serialized into XML. XmlSerializer uses opts-out approach i.e., marking the members do not need to be serialized.

3. XMLSerializer cannot translate the HashTable into XML.

4. XmlSerializer cannot serialize private members.

5. XmlSerializer can serialize only public types. If we are trying to serialize a class that is marked private, InvalidOperation Exception will be thrown by the serializer.

6. For any types that needs to be serialized by XmlSerializer must have a default constructor.

7. 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.

8. XmlSerializer cannot able to serialize types that implements IDictionary, for ex. Dictionary type cannot be serialized.

Happy Programming !!!

Jay Ganesh

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s