C# xml 序列化器 - 序列化派生对象
我想序列化以下内容:
[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] 时才会出现例外。
请帮忙!
谢谢!
I want to serialize the following:
[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
{
...
}
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
IvvalidOperationException.`there was an error generating the xml file `
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过编辑后的问题,看起来你有一个循环。请注意,
XmlSerializer
是一个tree 序列化程序,而不是graph 序列化程序,因此它会失败。这里通常的修复方法是禁用向上遍历:请注意,当然,您必须在反序列化后手动修复父级。
关于异常 - 您需要查看
InnerException
- 它可能准确地告诉您这一点,例如在您的(catch ex)
中:我猜它实际上是:
更多一般关于设计,老实说(公共字段、
ArrayList
、可设置列表)是不好的做法;这是一个更典型的重写其行为相同:根据要求,这是递归地将父母设置在蜂巢中的一般(非特定问题)说明(为了踢球,我在堆;对于bredth-first,只需将
Stack
交换为Queue
;在这些情况下,我尝试避免基于堆栈的递归):With the edited question, it looks like you have a loop. Note that
XmlSerializer
is a tree serializer, not a graph serializer, so it will fail. The usual fix here is to disable upwards traversal:Note you will have to manually fixup the parents after deserialization, of course.
Re the exception - you need to look at the
InnerException
- it probably tells you exactly this, for example in your(catch ex)
:I'm guessing it is actually:
More generally on the design, honestly that (public fields,
ArrayList
, settable lists) is bad practice; here's a more typical re-write that behaves identically:as requested, here's a general (not question-specific) illustration of recursively setting the parents in a hive (for kicks I'm using depth-first on the heap; for bredth-first just swap
Stack<T>
forQueue<T>
; I try to avoid stack-based recursion in these scenarios):