void
void

Reputation: 718

XML deserialization of derived classes

How can I create deserialization method that can take an object of class or any of derived classes?

public class Config
{
    public string appname;
}

public class CustomConfig1 : Config
{
    public string CustomConfig1Param1;
    public string CustomConfig1Param2;
}

public class CustomConfig2 : Config
{
    public string CustomConfig2Param1;
    public string CustomConfig2Param2;
}

I want to get something like serialization method that defines type of input object:

public string serialize(object obj)
{
    XmlSerializer serializer = new XmlSerializer(obj.GetType());
    StringWriter serialized = new StringWriter();
    serializer.Serialize(serialized, obj);
    return serialized.ToString();
}

But when I read an XML from DB I can't define the type of object, so I can't pass it to XmlSerializer. It may be the Config object or any of derived classes

Please help. How can I define the type of input object?

Upvotes: 5

Views: 5081

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1064004

[XmlInclude(typeof(CustomConfig1))]
[XmlInclude(typeof(CustomConfig2))]
public class Config
{
    public string appname;
}

Then just serialize/deserialize specifying typeof(Config); the library will give you back an instance of the appropriate type based on the data.


Edit: full example, including the preference to not hard-code the sub-types:

using System;
using System.IO;
using System.Xml.Serialization;

public class Config
{
    public string appname;
}

public class CustomConfig1 : Config
{
    public string CustomConfig1Param1;
    public string CustomConfig1Param2;
}

public class CustomConfig2 : Config
{
    public string CustomConfig2Param1;
    public string CustomConfig2Param2;
}

static class Program
{
    static void Main()
    {
        var original = new CustomConfig1
        {
            appname = "foo",
            CustomConfig1Param1 = "x",
            CustomConfig1Param2 = "y"
        };
        var xml = Serialize(original);
        var clone = DeserializeConfig(xml);
        Console.WriteLine(clone.appname);
        var typed = (CustomConfig1)clone;
        Console.WriteLine(typed.CustomConfig1Param1);
        Console.WriteLine(typed.CustomConfig1Param2);
    }
    public static string Serialize(Config obj)
    {
        using (var serialized = new StringWriter())
        {
            GetConfigSerializer().Serialize(serialized, obj);
            return serialized.ToString();
        }
    }
    public static Config DeserializeConfig(string xml)
    {
        using(var reader = new StringReader(xml))
        {
            return (Config)GetConfigSerializer().Deserialize(reader);
        }
    }
    static Type[] GetKnownTypes()
    {
        // TODO: resolve types properly
        return new[] { typeof(CustomConfig1), typeof(CustomConfig2) };
    }
    private static XmlSerializer configSerializer;
    public static XmlSerializer GetConfigSerializer()
    {
        return configSerializer ?? (configSerializer =
            new XmlSerializer(typeof(Config), GetKnownTypes()));
    }
}

Upvotes: 9

Related Questions