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!