I have an array of elements that I need to serialize using XmlSerializer. The problem I'm having is I have 2 derived classes, and serializing them so they have an element name of the common base, doesn't seem to work.
So, this is how the XML should look:
<Root>
<Base> foo </Base>
</Root>
Instead, I'm getting
<Root>
<Derived1> foo </Derived1>
</Root>
The code for the array of elements I am serializing is
private object[] m_nodes;
[System.Xml.Serialization.XmlElementAttribute("Base", typeof(Derived1)]
[System.Xml.Serialization.XmlElementAttribute("Base", typeof(Derived2)]
public object[] Nodes
{
get
{
return this.m_nodes;
}
set
{
this.m_nodes = value;
}
}
With the above code, I get that there is a reflection error with Nodes. If I change "Base" in both XmlEelementAttributes to "Derived1" and Derived2", it works, but the element name is incorrect.
[System.Xml.Serialization.XmlInclude(typeof(Derived1))]
public abstract class Base
{
public Base()
{
}
}
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public class Derived1: Base
{
public Derived1()
{
}
}
Any help is greatly appreciated. Thanks so much.