[Mono-list] Serializing a Graph ..

David Waite David Waite <dwaite@gmail.com>
Sun, 20 Feb 2005 20:56:44 -0700


the normal serialization is a bag of unique objects with references
between them; formatting with Xml implies a parent/child relationship.
The xml serializer as it exists does not make allowances to directly
create or use ID/IDREF relationships, which is what you really need
for your cyclic data structure.

-David Waite

On Mon, 21 Feb 2005 00:05:00 +0000, Phillip Neumann <bob@sofsis.cl> wrote:
> 
> 
> Ive just made the same with  BinaryFormatetr, instead of XmlSerialize
> 
> Why does it work? Why does XmlSerilize not work?
> 
> 
> Phillip Neumann wrote:
> 
> > Hello all...
> >
> > Im doing some work with Graph, so create some clases:
> >
> > Node
> > Arc
> > Graph.
> >
> > (A Graph is a list of Nodes)
> >
> > I like the idea about been able to save/load a graph to/from disk.
> > Im trying to XmlSerialize my graph.
> >
> > I have define my graph so, that this cannot be made, becouse:
> > 1.- Node = { X, Y, ListOfOutgoingArcs }
> > 2.- Arc    = { StartNode, StopNode, Weight}
> >
> > This definition is circular....
> >
> > What do u think is the best to modify, for let the graph been saved to
> > disk?
> >
> >
> > When i run this, i get this:
> > <mono1.0.5>
> > System.InvalidOperationException: A cirtular reference was detected
> > while serializing an object of type Node
> > </mono>
> >
> >
> > thanks in advance,
> >
> >
> >------------------------------------------------------------------------
> >
> >using System;
> >using System.Xml;
> >using System.Xml.Serialization;
> >using System.Collections;
> >
> >[Serializable, XmlInclude(typeof(Arc))]
> >public class Node{
> >
> >       public ArrayList OutGoingArcs = new ArrayList();
> >       public int X;
> >       public int Y;
> >       public Node(){}
> >       public Node(int x,int y){
> >               X=x;
> >               Y=y;
> >
> >       }
> >
> >}
> >
> >public class Arc{
> >       public int Weight;
> >       public Node StartAt;
> >       public Node EndAt;
> >       public Arc(){}
> >       public Arc(Node n1, Node n2){
> >               StartAt=n1;
> >               EndAt = n2;
> >               n1.OutGoingArcs.Add(this);
> >       }
> >
> >}
> >
> >[Serializable, XmlInclude(typeof(Node))]
> >public class Graf{
> >       public ArrayList Nodes;
> >
> >       public Graf (){
> >               Nodes = new ArrayList();
> >       }
> >
> >       public void Add(Node n){
> >               Nodes.Add(n);
> >       }
> >}
> >
> >public class M{
> >
> >       public static void Main(){
> >
> >               Graf g = new Graf();
> >
> >               Node n1 = new Node(1,1);
> >               Node n2 = new Node(2,2);
> >
> >               Arc a1=new Arc(n1,n2);
> >
> >
> >               g.Add(n1);
> >               g.Add(n2);
> >
> >               XmlSerializer seria = new XmlSerializer(typeof(Graf));
> >               seria.Serialize(Console.Out,g);
> >
> >
> >       }
> >
> >
> >}
> >
> >
> 
> --
> 
> _________________________
> Phillip Neumann
> phillip@sofsis.cl
> www.sofsis.cl
> 
> 
>