Is It possible to perform serialization with circular references?

大兔子大兔子 提交于 2019-11-29 11:57:50

问题


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.

This Parent property causes issues in serialization of the Object due to circular references.

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)

Any ideas on this?


回答1:


XML serialization doesn't support circular references, you need to exclude the parent property from the serialization using the XmlIgnore attribute. See this blog post for a way to maintain the relationship when you deserialize.

Alternatively, you could use DataContractSerializer instead of XmlSerializer. It supports circular references, but doesn't provide much control over the XML schema...




回答2:


You can either create your own XMLSerializer or use the DataContractSerializer and the [DataContract(IsReference= true)] attribute to tell the serializer to remember the references.



来源:https://stackoverflow.com/questions/5004397/is-it-possible-to-perform-serialization-with-circular-references

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!