[Mono-list] Serializing a Graph ..

Phillip Neumann bob@sofsis.cl
Mon, 21 Feb 2005 00:05:00 +0000


This is a multi-part message in MIME format.
--------------010109090509080800020803
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit



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


--------------010109090509080800020803
Content-Type: text/x-vcard; charset=utf-8;
 name="bob.vcf"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename="bob.vcf"

begin:vcard
fn:Phillip Neumann
n:Neumann;Phillip
email;internet:phillip@sofsis.cl
tel;cell:09-2750469
x-mozilla-html:TRUE
version:2.1
end:vcard


--------------010109090509080800020803--