I am new to serialization. Apologies, if my question sounds really
basic. Stuck in this problem for the last two hours. Thought, will ask
the experts instead of banging my head more.
This is what I have..!
<code>
Abstract Class FooBase
{
}
Abstract Class FoobaseCollection : ICollection, IEnumerable
{
private ArrayItem fooBaseList;
public foobase this[int index]
{
get
{
...
...
}
}
public void Add(FooBase object)
{
....
}
}
[XmlRoot("Foobar")]
Class Foobar : Foobase
{
}
[XmlRoot("FoobarCollection")]
Class FoobarCollection : FoobaseCollection
{
}
</code>
When I serialize the object using
<code>
FoobarCollection foobarCollection = new FoobarCollection();
foobarCollection.Add(new FooBase());
XmlSerializer serializer = new
XmlSerializer(typeof(FoobarCollection), new Type[] {typeof(Foobar)});
XmlTextWriter xmlTextWriter = new XmlTextWriter(@"C:\foobar.xml",
System.Text.Encoding.Default);
serializer.Serialize(xmlTextWriter, foobarCollection);
</code>
I dont get the expected xml. The internal arraylist elements are
serialized into tags with base class types(FooBase) rather than its
own type(FooBar).
eg:
<xml>
<FoobarCollection>
<FooBase ... />
<FooBase ... />
<FooBase ... />
</FoobarCollection>
instead of
<FoobarCollection>
<Foobar... />
<Foobar ... />
<Foobar ... />
</FoobarCollection>
</xml>
Am I missing something here. I cannot add the XmlElement or
XmlArrayList attribute to the base class property(public foobase
this[int index]), as I dont want to hardcode the type into some
inherited class name.
Is this a limitation in the pattern I have implemented?
Thanks!
-Karthick R