How to XML serialize polymorphic array without wrapping element
.net, c#, xml, xml-serialization
Solution
This was quite some time ago but I eventually figured it out on my own.
The solution was to add [XmlElement] attribute for each of the supported derived types to the collection property
private List<BaseCommand> _commands = new List<BaseCommand>();
[XmlElement(typeof(ExecuteCommand))]
[XmlElement(typeof(WaitCommand))]
public List<BaseCommand> Commands
{
get
{
return _commands;
}
set
{
_commands = value;
}
}
Problem
Want to serialise my data into this: ``` <?xml version="1.0" encoding="ibm850"?> <Batch Name="Test batch"> <ExecuteCommand Command="..." /> <WaitCommand Seconds="5" /> </Batch> ``` But instead I'm getting this (note the wrapping Commands element) ``` <?xml version="1.0" encoding="ibm850"?> <Batch Name="Test batch"> <Commands><!-- I want to get rid of thiw wrapper Commands element and just --> <ExecuteCommand Command="..." /> <WaitCommand Seconds="5" /> </Commands> </Batch> ``` Here's the sample code used to produce this: ``` public class BaseCommand //base class { [XmlAttribute] public string Result { get; set; } } public class ExecuteCommand : BaseCommand { [XmlAttribute] public string Command { get; set; } } public class WaitCommand : BaseCommand { [XmlAttribute] public int Seconds { get; set; } } public class Batch { [XmlAttribute] public string Name { get; set; } private List<BaseCommand> _commands = new List<BaseCommand>(); [XmlArrayItem(typeof(ExecuteCommand))] [XmlArrayItem(typeof(WaitCommand))] public List<BaseCommand> Commands { get { return _commands; } set { _commands = value; } } public static void Main() { XmlSerializer serializer = new XmlSerializer(typeof(Batch)); Batch b = new Batch(); b.Name = "Test batch"; b.Commands.Add(new ExecuteCommand() { Command = "..." }); b.Commands.Add(new WaitCommand() { Seconds = 5 }); serializer.Serialize(Console.Out, b); Console.Read(); } } ``` I searched and read heaps of articles on this topic. They all seem to provide solution for serializing collections with a single class type (no inheritance used). I use inheritance and nothing seems to work. Unfortunately I have to output precise XML document due to legacy support