We can manually add a type attribute to the xml token stream when parsing.
If you really really want to use deserialization, then I can suggest the following code.
Set of classes:
public class Profile
{
public List<ProfileSection> ProfileSections { get; set; }
}
[XmlInclude(typeof(DefaultProfileSection))]
[XmlInclude(typeof(AddressProfileSection))]
public class ProfileSection
{
[XmlAttribute]
public string Name { get; set; }
}
public class DefaultProfileSection : ProfileSection
{
public string Email { get; set; }
public string Password { get; set; }
}
public class AddressProfileSection : ProfileSection
{
public string Street { get; set; }
}
A custom xml reader that will add the necessary information on the fly:
public class TypeAddingXmlReader : XmlTextReader
{
public TypeAddingXmlReader(string url) : base(url) { }
// Define the remaining constructors here
public override string? GetAttribute(string localName, string? namespaceURI)
{
if (base.LocalName == "ProfileSection" &&
localName == "type" &&
namespaceURI == "http://www.w3.org/2001/XMLSchema-instance")
{
var name = base.GetAttribute("Name");
return name switch
{
"Default" => nameof(DefaultProfileSection),
"Address" => nameof(AddressProfileSection)
// add other class names here
};
}
return base.GetAttribute(localName, namespaceURI);
}
}
Use:
var ser = new XmlSerializer(typeof(Profile));
using var reader = new TypeAddingXmlReader("test.xml");
var profile = (Profile)ser.Deserialize(reader);
foreach (var ps in profile.ProfileSections)
Console.WriteLine(ps.Name + " " + ps.GetType());
Namespaces:
using System.Xml;
using System.Xml.Serialization;
Note: but I would use linq to xml and not sweat it.