Serialize into XML a list with multiple types without root element name



I'm trying to serialize my object below but I cannot serialize it without root name of list. Hub, Switch and Device are derived type from AbstractNode.



[XmlRoot(ElementName = "Roothub")]
public class RootHub
{
[XmlArrayItem(typeof(Hub), ElementName = "Hub20")]
[XmlArrayItem(typeof(Switch), ElementName = "Switch")]
[XmlArrayItem(typeof(Device), ElementName = "Device")]
public List<AbstractNode> DevicesList { get; set; }
}


Output:



<RootHub>
<DevicesList>
<Hub20 Tag="HUB1" VidPid="VID_0000&amp;PID_0000"/>
<Switch Tag="SWITCH1" />
<Device Tag="MOUSE" VidPid="VID_0000&amp;PID_0000"/>
</DevicesList>
</RootHub>


But what I need is this:



<RootHub>
<Hub20 Tag="HUB1" VidPid="VID_0000&amp;PID_0000"/>
<Switch Tag="SWITCH1" />
<Device Tag="MOUSE" VidPid="VID_0000&amp;PID_0000"/>
</RootHub>


I've already tried solution from this question Getting rid of an array name in C# XML Serialization but it won't work because it's not possible to mix XmlArrayItem with XmlElement attributes. Is there other way to do this?


No comments:

Post a Comment