5

I have a class Node something like this :

class Node {
    IEnumerable<Node> inputs;
}

Which basicly defines a simple graph. I want to serialize my graph to a human-readable form, so normally I'd say xml would be the way to go. But XML wasn't made with cyclic dependencies in mind :)

So - what would be the best way to go for serialization of my graph ?

I can think of a few ways :

  • ditch XML, create my own format.
  • use XML, tag each node with a unique ID, store connection-lists separate from the Nodes and resolve after loading

But I think other people must have had this same problem before, so there might be some better options. Does anyone know of a solid approach ?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Pygmy
  • 1,268
  • 17
  • 33

2 Answers2

3

For xml, I would go with the id approach (changing the DTO model such that it isn't cyclic).

Note that DataContractSerializer can support cyclic object graphs automatically by passing in true for the preserveObjectReferences option in the overloaded constructors; it won't be quite as simple as XmlSerializer output, but it will still be readable.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

If you switch to WCF DataContractSerializer, you can preserve the Object References (in 3.5 SP 1 and later)

[DataContract(IsReference=true)]

Sowmy has a good write up here

StuartLC
  • 104,537
  • 17
  • 209
  • 285