Serial versions of C #

I have the following situation. In my C # application, I have a class that I am serializing with XmlSerializer. The class is quite complex, and the object of my class is saved on the local disk as an application file, which can be opened later (classic save and re-work). My problem is that during development, the class of the object that is being serialized may change. I would like to have a version system that allows my application to understand that the saved xml belongs to the old version, but can be opened. Older versions of applications also cannot open new versions of xml.

For example:

class ComplexObject { public string settings1; public string settings2; } 

I serialize the object, send the application to production. Tomorrow my class became

 class ComplexObject { public string settings1; public string settings2; public string settings3; } 

How my new version of the application will open serialized objects of old class definitions, as well as a new class definition without errors when loading a file into an object (deserialization)

Any suggestions and basic samples are welcome!

thanks

+7
source share
4 answers

Vertical serialization

In short, you either mark the fields as Optional (and fill them with default values), or implement a deserialization constructor that will parse the values โ€‹โ€‹as you want.

+3
source

It all depends on the choice of serializer. In the case of the XmlSerializer this is normal and will work; Clients with a new value load a new value; there will be no customers without this. Example:

 var reader = XmlReader.Create(new StringReader( @"<ComplexObject><foo>123</foo><bar>abc</bar></ComplexObject>")); var ser = new XmlSerializer(typeof (ComplexObject)); var obj = (ComplexObject)ser.Deserialize(reader); 

from:

 public class ComplexObject { public string foo; } 

which works and loads foo , but not bar .

Do not use BinaryFormatter for this - this leads to a world of pain. If you want binary output, think of a proto-buf network that is designed to openly host the version.

+6
source

I hope I understood your problem correctly. You have a class serialized to a file. Then you change the class in memory (for example, add another property). No, you want to deserialize this class from a file. This is not a problem since you are adding only new properties. They will be ignored by the deserializer. It creates a new instance of your class (this is the reason why serializable classes should have a default constructor) and tries to populate the properties it finds in the stream for derserialize. If you change the type of a property or delete a property, you cannot deserialize it.

One way around the โ€œdelete propertiesโ€ workaround is perhaps to save the properties that you intentionally wanted to delete, and also to ignore them.

You can take a look at the version of Tolerant Serialization described in msdn http://msdn.microsoft.com/en-us/library/ms229752%28v=vs.80%29.aspx

+2
source

Track 1

You can create some if..else mechanism for opening a new version of files, which will try to open a file from the lowest possible version to a higher one.

Track 2

You can store version information in your files.

 class ComplexObject { public string settings1; public string settings2; public string fileVersion; } 

Track 3

You can use different file extensions for different file versions (e.g. .doc, .docx)

0
source

All Articles