Removing deserialization of the "old" xml into an instance of a new class

I had a class that looked like

public class MyClass { public string EmployerName; public string EmployerSurname; public string EmploeeName; public string EmploeeSurname; } 

I reworked the code above:

 public class MyClass { public MyClass() { Employer = new PersonInfo(); Emploee = new PersonInfo(); } public class PersonInfo { public string Name; public string Surname; } public PersonInfo Emploee; public PersonInfo Employer; [Obsolete] public string EmploeeName { get { return Emploee.Name; } set { Emploee.Name = value; } } [Obsolete] public string EmploeeSurname { get { return Emploee.Surname; } set { Emploee.Surname= value; } } [Obsolete] public string EmployerName { get { return Employer.Name; } set { Employer.Name = value; } } [Obsolete] public string EmployerSurname { get { return Employer.Surname; } set { Employer.Surname = value; } } 

The problem is that when deserializing XML that was serialized from an old version of the class, I was hoping that the new properties would work and the fields of the internal objects would be populated, but they didn't.

Any ideas on how, besides implementing IXmlSerializable, I could change the new class to support both new and old versions of XML? Or maybe IXmlSerializable is the only way?

+4
source share
1 answer

Do you only want to support the old ones for deserialization? if so, you could:

 [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] public string EmploeeName { get { return Emploee.Name; } set { Emploee.Name = value; } } public bool ShouldSerializeEmploeeName() { return false;} 

The bool method tells XmlSerializer never write this, but it will still be read. [Browsable] indicates that it does not appear in things like DataGridView , and [EditorBrowsable] indicates that it does not appear in intellisense (applies only to code that refers to the dll, not to the project, as well as code in the same project).

+5
source

All Articles