Possible Duplicate:
Replacement for XML Serialization
Is something new besides old XmlSerializer on the world of xml serialization in .Net universe?
Update: This is probably duplicate of better asked question.
Possible Duplicate:
Replacement for XML Serialization
Is something new besides old XmlSerializer on the world of xml serialization in .Net universe?
Update: This is probably duplicate of better asked question.
It's used by WCF by default for example. See here for a comparison.
Personally, I hate that XmlSerializer
can't do internal types. DataContractSerializer doesn't have that problem. Also, the DataContractSerializer
is easier with generics for example. If you want to produce a human editable XML format, it might be less than ideal.
As Maxim pointed out, the DataContractSerializer
behaves a little different from what you might expect: it doesn't call any constructor to deserialize your object. If you need to do some initialization for whatever reason, you can use the [OnDeserializing]
attribute. I use a pattern like this:
[DataContract]
public MyClass
{
public MyClass()
{
Initialize();
}
[OnDeserializing]
private OnDeserializing(StreamingContext context)
{
Initialize();
}
private void Initialize()
{
// Do stuff
}
}
Similarly, there is an [OnDeserialized]
attribute, and versions for serialization as well.