我想要序列化以下内容:
[Serializable]
[DefaultPropertyAttribute("Name")]
[XmlInclude(typeof(ItemInfo))]
[XmlInclude(typeof(ItemInfoA))]
[XmlInclude(typeof(ItemInfoB))]
public class ItemInfo
{
public string name;
[XmlArray("Items"), XmlArrayItem(typeof(ItemInfo))]
public ArrayList arr;
public 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
{
...
}
类itemInfo
描述了一个容器,可以在数组列表中容纳其他itemInfo
对象,parentItemInfo
描述了项目信息的父容器。
由于ItemInfoA
和ItemInfoB
派生自ItemInfo
,它们也可以成为数组列表和parentItemInfo
的成员,因此当尝试序列化这些对象(可以在层次结构中容纳许多对象)时,会出现异常。
IvvalidOperationException.`there was an error generating the xml file `
我的问题是:
我需要为ItemInfo
类添加哪些属性才能使其可序列化?
注意:只有在使用parentItemInfo
或arrayList初始化ItemInfo[A]/[B]时才会出现异常。
请帮忙解答!
谢谢!
XmlSerializer
进行序列化,因此在此情况下arr
永远 不会被序列化,parentItemInfo
也一样。我认为我们需要一个可重现的示例... 另外,虽然不重要,但XmlSerializer
没有使用[Serializable]
。 - Marc GravellArrayList
?你是在1.1吗? - Marc Gravell