XML : Deserialize XML from one class into a new one without old class in project

I have a project with a class that gets XML serialized, and this class has a property of another class type. I want to delete this class, and move the other class (the property class) to a new project and at the same time, change the structure of the property class and its children (add new properties, remove old ones, renames, etc). I know if I was just moving a class I could use the XmlType attribute on my class to get existing objects to deserialize into the new class / project / assembly, but this workflow seems more complex than that. Is it possible?

Some example code below.

The old class that I want to delete:

  [XmlInclude(typeof(BaseStyle))]  [XmlInclude(typeof(MyStyle))]  [XmlInclude(typeof(YourStyle))]  public class Node  {     public BaseStyle Style {get;set;}     // More stuff, most of it irrelevant  }    

The BaseStyle class and its children are defined as

  public class BaseStyle  {     public string Description = string.Empty;  }    public class MyStyle : BaseStyle  {     public double Factor1 = 1.0;     public double Factor2 = 2.0;  }    public class YourStyle : BaseStyle  {     public double Constant = 10.0;     public DefaultValuesClass DefaultValues = null;  }    

So all of that gets serialized into XML fine, and the resulting XML looks something like

  <Node>     <Style p2:type="YourStyle" xmlns:p2="blah">        <Constant>5.0</Constant>        <DefaultValues>         // XML for the DefaultValuesClass class here        </DefaultValues>     </Style>  </Node>    

Now, I've removed the Node class entirely (although there is an analog to it in the new assembly); moved the Style classes to another assembly; renamed them to BaseModel, MyModel, and YourModel; changed BaseModel and its Description property to be abstract; and changed YourModel so it now looks like:

  public sealed class YourModel : BaseModel  {     public double Constant {get; set;}     public bool UseDefaults {get; set;}     public NewDefaultValuesClass Defaults {get; set;}     public override Description => "This is your model";  }    

I want to map the <Constant> XML tag to YourModel.Constant, the <DefaultValues> XML tag to YourModel.UseDefaults and YourModel.Defaults, and provide a predefined prescription based on the fact that it is of type YourStyle (or just use the default provided in YourModel).

If I kept Node around (and maybe BaseStyle and its children?), I could write some custom deserialization code, or write a conversion utility to run from a context menu or during loading, but I'd rather not keep Node. I'd also rather not rename any of the XML elements to be "Node" as that would get confusing down the road. Since I know the structure of the existing XML, is there a way I can use it to populate new MyModel and YourModel objects, without having the class definition of Node?

I hope this all made sense, please leave a comment if I need to clarify.

No comments:

Post a Comment