Maintain object references through Serialize/Deserialize

后端 未结 4 1089
渐次进展
渐次进展 2021-01-20 19:57

When serializing, I would like to serialize an object only once, then any references to that object should be serialized as a reference to the object. This is because, when

4条回答
  •  别那么骄傲
    2021-01-20 20:33

    your code example won't work simply because the XmlSerializer does not save reference information, during the serialization process it will simply write the data for obj to the stream twice this means that when you deserialize it will read the data for obj as two different instances, it has no way of knowing that they referred to the same instance at serialization time. using the DataContractSerializer mentioned before should give you what you want but note that it uses non standard xml (see: http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx)

提交回复
热议问题