简体   繁体   English

是否可以使用循环引用执行序列化?

[英]Is It possible to perform serialization with circular references?

So, my entity class (written in C#) follows a parent child model where every child object must have a Parent property in which it keeps reference of its Parent. 因此,我的实体类(用C#编写)遵循父子模型,其中每个子对象必须具有Parent属性,在该属性中它保持对其Parent的引用。

This Parent property causes issues in serialization of the Object due to circular references. 由于循环引用,此Parent属性导致Object序列化的问题。

I can't remove the reference to parent, neither I can mark it XmlIgnore (since I need to read it back when I deserialize the XML) 我无法删除对父级的引用,也不能将其标记为XmlIgnore(因为我在反序列化XML时需要将其读回)

Any ideas on this? 有什么想法吗?

XML serialization doesn't support circular references, you need to exclude the parent property from the serialization using the XmlIgnore attribute. XML序列化不支持循环引用,您需要使用XmlIgnore属性从序列化中排除父属性。 See this blog post for a way to maintain the relationship when you deserialize. 有关在反序列化时维护关系的方法,请参阅此博客文章

Alternatively, you could use DataContractSerializer instead of XmlSerializer . 或者,您可以使用DataContractSerializer而不是XmlSerializer It supports circular references, but doesn't provide much control over the XML schema... 它支持循环引用,但不能提供对XML模式的很多控制......

您可以创建自己的XMLSerializer也可以使用DataContractSerializer[DataContract( IsReference = true)]属性来告诉序列化程序记住引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM