C# xml serializer - serialize derived objects
Posted
by
gln
on Stack Overflow
See other posts from Stack Overflow
or by gln
Published on 2011-02-01T07:17:41Z
Indexed on
2011/02/01
7:25 UTC
Read the original article
Hit count: 181
Hi,
I want to serialize the following:
[Serializable]
[DefaultPropertyAttribute("Name")]
[XmlInclude(typeof(ItemInfo))]
[XmlInclude(typeof(ItemInfoA))]
[XmlInclude(typeof(ItemInfoB))]
public class ItemInfo
{
private string name;
[XmlArray("Items"), XmlArrayItem(typeof(ItemInfo))]
private ArrayList arr;
private ItemInfo parentItemInfo;
}
[Serializable]
[XmlInclude(typeof(ItemInfo))]
[XmlInclude(typeof(ItemInfoA))]
[XmlInclude(typeof(ItemInfoB))]
public class ItemInfoA : ItemInfo
{
...
}
[Serializable]
[XmlInclude(typeof(ItemInfo))]
[XmlInclude(typeof(ItemInfoA))]
[XmlInclude(typeof(ItemInfoB))]
public class ItemInfoB : ItemInfo
{
...
}
The class itemInfo
describes a container which can hold other itemInfo
objects in the array list, the parentItemInfo
describes which is the parent container of the item info.
Since ItemInfoA
and ItemInfoB
derive from ItemInfo
they can also be a member of the array list and the parentItemInfo
, therefore when trying to serialize these objects (which can hold many objects in hierarchy) it fails with exception
can't generate the xml file - innerexception.
My question is:
What attributes do I need to add the ItemInfo
class so it will be serializable?
Note: the exception is only when the ItemInfo[A]/[B] are initialized with parentItemInfo
or the arrayList.
Help please!
Thanks!
© Stack Overflow or respective owner