views:

26

answers:

1

This question does a good job of explaining the difference in functionality between the serializers. BinaryFormatter is fast, XmlSerializer is interoperable, etc. I know that.

But what is the difference in intent? What use-case was each class designed for?

In particular:

  • Why did they decide to make XmlSerializer blind for private data, when BinaryFormatter sees it fast enough (via the supposedly-slow reflection)
  • Why did they make 3 separate XML serializers - XmlSerializer, SoapFormatter and DataContractSerializer?
  • Why are some opt-in and some opt-out?
  • Why did they make the interfaces so inconsistent? E.g. XmlSerializer doesn't provide an OnDeserializedCallback equivalent. Another example - XmlSerializer uses IsNullable [XmlIgnore] for what BinarySerializer uses [OptionalField] and [NonSerialized].
A: 

XML Serialization is used to create a specific XML structure, and allows you to change how the object is serialized - what the XMl looks like (if you use attributes or nodes, etc).

Binary Serialization "sees" the private members because it just takes the in memory structure of the object and writes it to to disk. Very fast, but not interoperable, especially with slightly different object structures (like a new version of your app attempting to deserialize an older binary file).

The SoapFormatter and DataContractSerializer are used for communication between .NET components, normally a tiered application - with DataContractSerializer used in WCF, and SoapFormatter used for the older SOAP utilities.

davisoa