views:

971

answers:

2

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!

+3  A: 

You simply have to use multiple [XmlInclude] attributes. This works well.

John Saunders
+7  A: 

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);
    }
}
Marc Gravell
I tried the following on the Profiles property, which did not work: [XmlInclude(typeof(IncomeProfile)), XmlInclude(typeof(CostProfile))]However when I now set them on the Profile class it worked. I was so close, and have spent so many hours trying to write my own implementation of the serialization methods. Kind of irritating, but thanks alot :)
atsjoo
Is there any difference between the following?A: [XmlInclude(typeof(IncomeProfile)), XmlInclude(typeof(CostProfile))]B: [Xmlinclude(typeof(IncomeProfile))][Xmlinclude(typeof(CostProfile))]
atsjoo
No difference whatsoever.
Marc Gravell