Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing a List<T> to XML with inheritance

I've run into some problems when trying to serialize my object to XML. The problem appears when trying to serialize a "Profiles" property which is a List of Profile items. Profile is my own type. The Profile type should ideally be abstract, but it isn't, since XML serialization demands a parameterless ctor. The Profiles property contains items of type "IncomeProfile", "CostProfile", "InvestmentProfile" etc, which all of course inherits from Profile.

As I've read up to, serializing this is not natively supported, since the XmlIncludeAttribute only allows one inherited type. I.e.

[XmlInclude(typeof(IncomeProfile))]
public List<Profile> Profiles { get; set; }

What is the best practice when solving this problem? I've tried different solutions using IXmlSerializable and reflection, however I can't seem to deserialize each profile to the correct type (they all end up using the ReadXml(XmlReader reader) method of the Profile type, even though the Visual Studio debugger says the type of the object is "IncomeProfile" or "CostProfile". This is my current deserialization code, which deserializes the xml into three Profile objects, instead of two IncomeProfile and one CostProfile:

while(reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == "Profile")
    {
        String type = reader["Type"];
        var project = (Profile)Activator.CreateInstance(Type.GetType(type));
        project.ReadXml(reader);

        reader.Read();
        this.Profiles.Add(p2);
    }

Any thoughts or suggestions are very much appreciated!

like image 586
atsjoo Avatar asked Dec 14 '22 04:12

atsjoo


1 Answers

You are allowed to use multiple include attributes - although they are more commonly set against the type itself:

using System;
using System.Collections.Generic;
using System.Xml.Serialization;
[XmlInclude(typeof(IncomeProfile))]
[XmlInclude(typeof(CostProfile))]
[XmlInclude(typeof(InvestmentProfile))]
public class Profile {
    public string Foo { get; set; }
}
public class IncomeProfile : Profile {
    public int Bar { get; set; }
}
public class CostProfile : Profile { }
public class InvestmentProfile : Profile { }
static class Program {
    static void Main() {
        List<Profile> profiles = new List<Profile>();
        profiles.Add(new IncomeProfile { Foo = "abc", Bar = 123 });
        profiles.Add(new CostProfile { Foo = "abc" });
        new XmlSerializer(profiles.GetType()).Serialize(Console.Out, profiles);
    }
}
like image 159
Marc Gravell Avatar answered Dec 22 '22 00:12

Marc Gravell