I used Eric's solution with several modifications. I added an interface class to handle the backward compatibility part.
public interface IBackwardCompatibilitySerializer { void OnUnknownElementFound(string uknownName, string value); }
Using this, we need to record an unknown element event only once, like this
private static void x_UnknownElement(object sender, XmlElementEventArgs e) { var deserializedObj = (e.ObjectBeingDeserialized as IBackwardCompatibilitySerializer); if (deserializedObj == null) return; deserializedObj.OnUnknownElementFound(e.Element.Name, e.Element.InnerText); }
Then, for any class in which you want to change the variable name, execute its interface. Then your class will look like this:
public class MyClass : IBackwardCompatibilitySerializer { // public bool Included { get; set; } Old variable public bool IsIncluded { get; set; } // New Variable public void OnUnknownElementFound(string unknownElement, string value) { switch(unknownElement) { case "Included": IsIncluded = bool.Parse(value); return; } } }
Eric, feel free to include this in your decision if you want.
source share