I have a class below which I need to serialize using an XML serializer
I know that the XML serializer does not like interfaces
However, our coding standards usually always include coding for interfaces, not specific classes.
I thought about how to pass this list without changing the types, which can cause many compilation problems, and also violate our standards.
My idea is to have a property that reads a list of counters and discards specific classes, and then completely ignores meters when it comes to serialization
However, XML should still have Meters as the element name
However this does not work
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Xml.Serialization;
public class MeterWalkOrder
{
public MeterWalkOrder()
{
Meters = new List<IMeter>();
}
public String Name { get; set; }
[XmlIgnore]
public List<IMeter> Meters { get; set; }
[XmlArrayItem(ElementName = "Meter")]
[XmlElement(ElementName = "Meters")]
public List<Meter> SerializableMeters
{
get
{
return Meters.Cast<Meter>().ToList();
}
set
{
Meters = new List<IMeter>(value);
}
}
}
}
Is there any way around this?
My XML (which cannot be modified) is below
<MeterWalkOrder>
<Name>Red Route</Name>
<Meters>
<Meter>
<MeterID>1</MeterID>
<SerialNumber>12345</SerialNumber>
</Meter>
<Meter>
<MeterID>2</MeterID>
<SerialNumber>SE</SerialNumber>
</Meter>
</Meters>
</MeterWalkOrder>
, "WindowsFormsApplication1.Classes.MeterWalkOrder".
{ "XmlElement, XmlText XmlAnyElement XmlAttribute, XmlAnyAttribute, XmlArray XmlArrayItem." }