I’ve been reading lately about serialization. I’ve read that when I use XmlSerialization I cannot serialize object graphs. What is an object graph and why I cannot serialize it simply?
I’ve been reading lately about serialization. I’ve read that when I use XmlSerialization I
Share
An object graph is not a single object, but rather a set of related objects. For a simple example, consider:
where each child knows about the parent (and the parent knows about the child).
The problem is that xml is a tree based on object properties… and it wants to just walk them – i.e. with the simple parent/child:
that would serialize as:
You can see that we got back to A, so we’re now in an endless loop.
XmlSerializercan serialize trees of data, but not full graphs. You can mark properties to be ignored, for example:And now it’ll work, but we’ll have to fix the
Parentafterwards.By contrast, some other serializers can handle graphs (
DataContractSerializercan on-demand). It does this by tracking objects against a unique key – but then the output isn’t what you expect from regular xml.