简体   繁体   中英

XML version-specific deserialization

So, I have a base project and several modified version of it. BaseProject contains class BaseClass ,

namespace BaseProject.SomeClasses{
    public abstract class BaseClass{
    //...
    }
}

while each of the versions contain several inheritors of BaseClass - fe ProjectOne :

namespace BaseProject.VersionOne.SomeClasses{
    public class InheritorClass : BaseClass{
    //Some logic here...
    }
}

and ProjectTwo :

namespace BaseProject.VersionTwo.SomeClasses{
    public class InheritorClass : BaseClass{
    //Some different logic here...
    }
}

The only thing that differs is the name of the namespace.

Base project loads each of the assemblies during the runtime and gets all of the inherited types. I need to create the xml file, which should contain both of the inheritors' instances and some pointers to the class which the instance should be deserialized into:

...
<BaseClass xsi:type="InheritorClass"> <!-- From VersionOne -->
    <PropOne></PropOne>
    <PropTwo></PropTwo>
    <PropThree></PropThree>
    <!-- ... -->
</BaseClass>
<BaseClass xsi:type="InheritorClass"> <!-- From VersionTwo -->
    <PropFour></PropFour>
    <PropFive></PropFive>
    <PropSix></PropSix>
    <!-- ... -->
</BaseClass>
...

Is there any way to deserialize this xml (which contains the instances of inheritors from both of the versions) into IEnumerable<BaseClass> ?

The namespace of the class shouldn't make a difference. See XML file generate by this code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            Root root = new Root()
            {
                baseClass = new List<BaseClass>(){
                    new InheritorClass1(){ name = "class1"},
                    new InheritorClass2(){ name = "class2"}
                }
            };
            XmlSerializer serializer = new XmlSerializer(typeof(Root));

            StreamWriter writer = new StreamWriter(FILENAME);
            serializer.Serialize(writer, root);
            writer.Flush();
            writer.Close();
            writer.Dispose();

            XmlSerializer xs = new XmlSerializer(typeof(Root));
            XmlTextReader reader = new XmlTextReader(FILENAME);
            Root newRoot = (Root)xs.Deserialize(reader);
        }
    }
    [XmlRoot("Root")]
    public class Root
    {
        [XmlElement("BaseClass")]
        public List<BaseClass> baseClass { get; set; }
    }
    [XmlRoot("BaseClass")]
    [XmlInclude(typeof(InheritorClass1))]
    [XmlInclude(typeof(InheritorClass2))]
    public class BaseClass
    {
        [XmlElement("name")]
        public string name { get; set; }    

    }
    [XmlRoot("InheritorClass1")]
    public class InheritorClass1 : BaseClass
    {
    }

    [XmlRoot("InheritorClass2")]
    public class InheritorClass2 : BaseClass
    {
    }

}
​

You need to apply the [XmlTypeAttribute(name)] attribute to your derived classes to disambiguate the xst:type names:

namespace BaseProject.VersionOne.SomeClasses
{
    [XmlType("VersionOneInheritorClass")]
    public class InheritorClass : BaseClass
    {
        public string VersionOneProperty { get; set; } // For instance
    }
}

and

namespace BaseProject.VersionTwo.SomeClasses
{
    [XmlType("VersionTwoInheritorClass")]
    public class InheritorClass : BaseClass
    {
        public string VersionTwoProperty { get; set; } // For instance
    }
}

Then your XML will appear as follows, and can be serialized and deserialized successfully without loss of information:

 <BaseClass xsi:type="VersionOneInheritorClass"> <VersionOneProperty>one</VersionOneProperty> </BaseClass> <BaseClass xsi:type="VersionTwoInheritorClass"> <VersionTwoProperty>two</VersionTwoProperty> </BaseClass> 

By the way, if you are constructing an XmlSerializer using the XmlSerializer (Type, Type[]) constructor to add in your discovered derived types, you must cache the serializer in a static cache somewhere , or else you will have a horrible resource leak.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM