Is there exists some way how to make a updatable and serializable class? c#

34 Views Asked by At

I'm currently have a one class Profile which has a Name. But after some time, some changes are required (for example, to replace the Name property with a new class PersonalInfo) in the class after which the data will be lost when deserializing the old class into the new one or it will lead to the impossibility to deserialize the file at all.

//Old class
[Serializable, XmlRoot] public class Profile
{
    [XmlAttribute] public string Name { get; set; }
}
//New class
[Serializable, XmlRoot] public class Profile
{
    [XmlElement] public PersonalInfo PersonalInfo { get; set; }
}
//New element which will replace old "Name" Attribute
[Serializable, XmlRoot] public class PersonalInfo
{
    [XmlAttribute] public string Name { get; set; }
}

I have an idea to create a different Name for the XmlRoot attribute for the new version so that the attempt to deserialize the new version would cause an error (if the file describes the old version) after which the program would have to read the data into the old class and then transfer them to the new one programmatically.

This approach worked, but this solution will lead to additional work in the future if there are additional changes in the class. That's why I'm thinking now about how to make this process less laborious and that in case the class has more than two versions (1, 2, 3) then the data transfer would take place according to the order from 1 to 2 and to 3.

0

There are 0 best solutions below