How to rename an enumeration that is serialized in XML, but continue to support previous names?

I have an enumeration that describes the options available to the user to select as part of the settings. It is serialized in XML. One of the names is not ideal, and I would like to rename it, but still support the deserialization of the old settings files.

For instance:

public enum Options { Odd, NonOdd // rename to 'Even' } 

I know that I can rename it, but specify the previous serialized name as follows:

 public enum Options { Odd, [XmlEnum(Name = "NonOdd")] Even } 

While this works, he continues to use NonOdd in the XML file, which I would rather not do.

Is there a way to support deserialization of current and obsolete enumeration names, but serialized for the current name?

+5
source share
1 answer

Here is what I will do:

  • Add version number to settings file.
  • Enter the code that converts the files from the old version of XML to the current version.
  • Update listing and increase version number.

Once you create this structure, you can rename the enumeration elements as many times as you want.

+1
source

Source: https://habr.com/ru/post/1264796/


All Articles