none
Deserializing from old serialized data RRS feed

  • Question

  • I am a developer for an application that uses the NetDataContractSerializer to serialize a set of related objects, which are retrieved from a database via Entity Framework, containing data for medical studies.  The classes all have the [DataContract] attribute.  The serialized data is saved to an archive file that can be restored on another identical system to rehydrate the objects, save to that system's database and examine the study results on that system. 

    As the system has evolved, we have added fields, new objects and created new relationships between the data.  The problem is that I need to be able to restore old archive files on a newer version of the software.  I have read about Version Tolerant serialization, but this seems to apply to classes that use the [Serializable] attribute.  I am using the [DataContract] attribute and the NetDataContractSerializer.

    The only solutions I see are to either write my own custom XML reader and create the objects with explicit code, or somehow maintain older versions of all the assemblies in some kind of isolated environment where I can rehydrate the older objects and write some kind of translator.  This seems like a common problem that must have a better solution.

    Wednesday, May 20, 2015 5:58 PM

Answers

  • hi john,
      As per this case, I have shared corresponding details below : 

    The simplest way to deserialize an object is to call one of the ReadObject method overloads. Also note that the object returned by the ReadObject method must be cast to the appropriate type:

    DataContractSerializer dcs = new DataContractSerializer(typeof(Person));
     FileStream fs = new FileStream(path, FileMode.Open);
     XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());
     Person p = (Person)dcs.ReadObject(reader);

    The XML formats that the NetDataContractSerializer and the DataContractSerializer use are normally not compatible. That is, attempting to serialize with one of these serializers and deserialize with the other is not a supported scenario.

    Also, note that the NetDataContractSerializer does not output the full .NET Framework type and assembly name for each node in the object graph. It outputs that information only where it is ambiguous. That is, it outputs at the root object level and for any polymorphic cases

    The NetDataContractSerializer differs from the DataContractSerializer in that it includes the CLR type information in the serialized XML and can only be used if the same CLR types are serialized and deserialized at both ends.

    for more information ,Click here to refer .

    Thursday, May 21, 2015 5:46 AM