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.
3.The DataContractSerializer can translate the HashTable into XML.
4.DataContractSerializer is basically for very small, simple subset of the XML infoset.
5.DataContractSerializer serializes private members.
6.DataContractSerializer uses the opts-in approach i.e, selecting the members that needs to be serialized .This is called as opts-in approach.
7.DataContractSerializer can serialize both private and public types.
8.DataContractSerializer does not need any default constructor before serializing any type.
9.DataContractSerializer does not give more control over the generated xml structure compared to the XmlSerializer.
10.DataContractSerializer can able to serialize types that implements Idictionary.
11.We can serialize a type that marked with [Serializable] attribute with DataContractSerializer. It serializes all the members (private, public) even they are marked with [XmlIgnore].
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
3.XMLSerializer cannot translate the HashTable into XML.
4.XMLSerializer is used for complex schemas.
5.XmlSerializer cannot serialize private members
6.XmlSerializer uses opts-out approach i.e., marking the members do not need to be serialized. This is called as opts-out approach.
7.XmlSerializer can serialize only public types. If we are trying to serialize a class that is marked private by InvalidOperation Exception will be thrown by the serializer.
8.For any types that needs to be serialized by XmlSerializer must have a default constructor.
9. 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.
10.XmlSerializer cannot able to serialize types that implements IDictionary, for ex. Dictionary type cannot be serialized.
11.Only the public members are serialized not the private members. Suppose we do not need any of the member to be serialized we can use [XmlIgnore] attribute