I have a situation where I serialize some .NET objects using the NetDataContractSerializer and save the XML in the database to remember the state of these objects in the application. Recently, I came across the first situation where some refactoring of property code and type names led to the rejection of deserializing XML data.
So far, I have come up with two different attack plans for how to deal with version compatibility gaps, such as those that should use the tools available in the NetDataContractSerializer itself, to control deserialization or just to directly convert XML. From my experiments and research, it seems that you can deserialize to another type using a custom SerializationBinderand property / type name changes can be eliminated by implementing ISerializable or writing a serialization surrogate by implementing ISurrogateSelector and ISerializationSurrogate. Unfortunately, this preferred mechanism has not been disabled, and if I can not show it otherwise, then using surrogates, the transition between the serialized version of the data is not possible using the NetDataContractSerializer, and this is due to some inexplicable design solution from Microsoft . Microsoft suggested using the same serialization on both sides, which completely violates the purpose of using a surrogate to help in cases where the type name changes or moves to another namespace or assembly.
To fix this, use the same instance of NetDataContractSerializer or another instance that is also initialized by a compatible SurrogateSelector.
This explanation contradicts an MSDN article that talks about using a custom binder to replace types along with other changes to the serialization structure.
During deserialization, the formatter sees that a binder is installed. Since each object is about to be deserialized, the formatter calls BinderToType, passing it the name and type of assembly that the formatter wants to deserialize. At the moment, BindToType decides which type should be built and returns this type.
, , . ISerializable, , SerializationInfo , .
NetDataContractSerializer , V1 XML V2, XML. - , NetDataContractSerializer SerializationInfo ISerializable , , , , , Microsoft, , , , .NET XML.
2011-08-16:
, ISerializable , , , ISerializable, , [Serializable], , .
[Serializable]
<OldClass2 xmlns:i="http://www.w3.org/2001/XMLSchema-instance" z:Id="1" z:Type="NdcsSurrogateTest.OldClass2" z:Assembly="NdcsSurrogateTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns="http://schemas.datacontract.org/2004/07/NdcsSurrogateTest">
<_stable z:Id="2">Remains the same</_stable>
<_x003C_OldProperty_x003E_k__BackingField>23</_x003C_OldProperty_x003E_k__BackingField>
</OldClass2>
ISerialzable:
<OldClass2 xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="http://www.w3.org/2001/XMLSchema" z:Id="1" z:Type="NdcsSurrogateTest.OldClass2" z:Assembly="NdcsSurrogateTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns="http://schemas.datacontract.org/2004/07/NdcsSurrogateTest">
<_stable z:Id="2" z:Type="System.String" z:Assembly="0" xmlns="">Remains the same</_stable>
<_x003C_OldProperty_x003E_k__BackingField z:Id="3" z:Type="System.Int32" z:Assembly="0" xmlns="">23</_x003C_OldProperty_x003E_k__BackingField>
</OldClass2>
NetDataContractSerializer , ISerializable , , , ISerializalbe, SerializationInfo ISerializationSurrogate.SetObjectData. xml SerializationInfo, , , , .
, XML , NetDataContractSerializer , SerializableAttribute, ISerializable - . , NetDataContractSerializable , , , XML, XML.