5

I have an enum which describes options available for a user to select as part of settings. This is serialized to XML. One of the names is not ideal, and I'd like to rename it, but still support deserialization of older settings files.

For example:

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

I know I can rename it but specify the previous serialized name like this:

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

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

Is there a way to support deserialization of current and deprecated enum names, but serialize to the current name?

atoMerz
  • 7,534
  • 16
  • 61
  • 101
JYelton
  • 35,664
  • 27
  • 132
  • 191
  • 1
    Possible duplicate of [How to define multiple names for XmlElement field?](http://stackoverflow.com/questions/24707399/how-to-define-multiple-names-for-xmlelement-field) – CoolBots Feb 27 '17 at 19:23
  • Just tested [How to define multiple names for XmlElement field?](http://stackoverflow.com/q/24707399/3744182). Unfortunately it seems as though neither the `UnknownNode`, `UnknownElement`, `UnknownAttribute` nor `UnreferencedObject` events are raised for an unknown enum value. Thus that solution will not work here. Instead, the solutions from [XmlSerializer with new enum values](http://stackoverflow.com/q/1621306/3744182) or [Handling extra spaces when deserializing XML values to enums](http://stackoverflow.com/a/31253230/3744182) may be the best way to go. – dbc Feb 27 '17 at 19:47

1 Answers1

2

Here's what I would do:

  1. Add a version number to the settings file.
  2. Write code that converts older version XML files to the current version.
  3. Update enum and increase the version number.

Once you have this structure in place you can rename enum elements as many times as you want.

atoMerz
  • 7,534
  • 16
  • 61
  • 101