I have XML in a format like so:
<TaskList>
<Task type="TaskA"/>
<Task type="TaskB"/>
</TaskList>
that I am trying to deserialize into classes like so:
public class TaskList
{
[XmlElement(ElementName = "Task", Type = typeof(Task))]
public Task[] Task { get; set; }
}
[XmlInclude(typeof(TaskA))]
[XmlInclude(typeof(TaskB))]
[XmlRoot(ElementName = "Task")]
public class Task
{
//Common Fields
}
public class TaskA : Task
{
//Specific stuff
}
public class TaskB : Task
{
//Specific stuff
}
Trying to deserialize it:
TaskList taskList = (TaskList)new XmlSerializer(typeof(TaskList)).Deserialize(new StringReader(xmlString));
In this configuration it'll give me all of the tasks as the base type "Task". How can I get it to use the derived classes?