I got the error "{"The type Device1 was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically."}"
Currently I have:
public abstract class Device
{
..
}
public class Device1 : Device
{ ... }
[Serializable()]
public class DeviceCollection : CollectionBase
{ ... }
[XmlRoot(ElementName = "Devices")]
public class XMLDevicesContainer
{
private DeviceCollection _deviceElement = new DeviceCollection();
/// <summary>Devices device collection xml element.</summary>
[XmlArrayItem("Device", typeof(Device))]
[XmlArray("Devices")]
public DeviceCollection Devices
{
get
{
return _deviceElement;
}
set
{
_deviceElement = value;
}
}
}
and i am doing:
XMLDevicesContainer devices = new XMLDevicesContainer();
Device device = new Device1();
device.DeviceName = "XXX";
device.Password = "Password";
devices.Devices.Add(device);
Serializer.SaveAs<XMLDevicesContainer>(devices, @"c:\Devices.xml", new Type[] { typeof(Device1) });
serializer does:
public static void Serialize<T>(T obj, XmlWriter writer, Type[] extraTypes)
{
XmlSerializer xs = new XmlSerializer(typeof(T), extraTypes);
xs.Serialize(writer, obj);
}
I fall on the last line in the serializer method (xs.Serialize) on the error: "{"The type Device1 was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically."}"
I tried to write XmlInclude on the Device class. not helped. If I change the line
[XmlArrayItem("Device", typeof(Device))]
to be
[XmlArrayItem("Device", typeof(Device1))]
then it works, but I want to write array of multiple Device types.