mail-archives/mono-list/2005-February/025880.html

188 строки
6.3 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE> [Mono-list] Serializing a Graph ..
</TITLE>
<LINK REL="Index" HREF="index.html" >
<LINK REL="made" HREF="mailto:David%20Waite%20%3Cdwaite%40gmail.com%3E">
<META NAME="robots" CONTENT="index,nofollow">
<LINK REL="Previous" HREF="025879.html">
<LINK REL="Next" HREF="025937.html">
</HEAD>
<BODY BGCOLOR="#ffffff">
<H1>[Mono-list] Serializing a Graph ..
</H1>
<B>David Waite
</B>
<A HREF="mailto:David%20Waite%20%3Cdwaite%40gmail.com%3E"
TITLE="[Mono-list] Serializing a Graph ..">David Waite &lt;dwaite@gmail.com&gt;
</A><BR>
<I>Sun, 20 Feb 2005 20:56:44 -0700</I>
<P><UL>
<LI> Previous message: <A HREF="025879.html">[Mono-list] Serializing a Graph ..
</A></li>
<LI> Next message: <A HREF="025937.html">[Mono-list] Serializing a Graph ..
</A></li>
<LI> <B>Messages sorted by:</B>
<a href="date.html#25880">[ date ]</a>
<a href="thread.html#25880">[ thread ]</a>
<a href="subject.html#25880">[ subject ]</a>
<a href="author.html#25880">[ author ]</a>
</LI>
</UL>
<HR>
<!--beginarticle-->
<PRE>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 &lt;<A HREF="mailto:bob@sofsis.cl">bob@sofsis.cl</A>&gt; wrote:
&gt;<i>
</I>&gt;<i>
</I>&gt;<i> Ive just made the same with BinaryFormatetr, instead of XmlSerialize
</I>&gt;<i>
</I>&gt;<i> Why does it work? Why does XmlSerilize not work?
</I>&gt;<i>
</I>&gt;<i>
</I>&gt;<i> Phillip Neumann wrote:
</I>&gt;<i>
</I>&gt;<i> &gt; Hello all...
</I>&gt;<i> &gt;
</I>&gt;<i> &gt; Im doing some work with Graph, so create some clases:
</I>&gt;<i> &gt;
</I>&gt;<i> &gt; Node
</I>&gt;<i> &gt; Arc
</I>&gt;<i> &gt; Graph.
</I>&gt;<i> &gt;
</I>&gt;<i> &gt; (A Graph is a list of Nodes)
</I>&gt;<i> &gt;
</I>&gt;<i> &gt; I like the idea about been able to save/load a graph to/from disk.
</I>&gt;<i> &gt; Im trying to XmlSerialize my graph.
</I>&gt;<i> &gt;
</I>&gt;<i> &gt; I have define my graph so, that this cannot be made, becouse:
</I>&gt;<i> &gt; 1.- Node = { X, Y, ListOfOutgoingArcs }
</I>&gt;<i> &gt; 2.- Arc = { StartNode, StopNode, Weight}
</I>&gt;<i> &gt;
</I>&gt;<i> &gt; This definition is circular....
</I>&gt;<i> &gt;
</I>&gt;<i> &gt; What do u think is the best to modify, for let the graph been saved to
</I>&gt;<i> &gt; disk?
</I>&gt;<i> &gt;
</I>&gt;<i> &gt;
</I>&gt;<i> &gt; When i run this, i get this:
</I>&gt;<i> &gt; &lt;mono1.0.5&gt;
</I>&gt;<i> &gt; System.InvalidOperationException: A cirtular reference was detected
</I>&gt;<i> &gt; while serializing an object of type Node
</I>&gt;<i> &gt; &lt;/mono&gt;
</I>&gt;<i> &gt;
</I>&gt;<i> &gt;
</I>&gt;<i> &gt; thanks in advance,
</I>&gt;<i> &gt;
</I>&gt;<i> &gt;
</I>&gt;<i> &gt;------------------------------------------------------------------------
</I>&gt;<i> &gt;
</I>&gt;<i> &gt;using System;
</I>&gt;<i> &gt;using System.Xml;
</I>&gt;<i> &gt;using System.Xml.Serialization;
</I>&gt;<i> &gt;using System.Collections;
</I>&gt;<i> &gt;
</I>&gt;<i> &gt;[Serializable, XmlInclude(typeof(Arc))]
</I>&gt;<i> &gt;public class Node{
</I>&gt;<i> &gt;
</I>&gt;<i> &gt; public ArrayList OutGoingArcs = new ArrayList();
</I>&gt;<i> &gt; public int X;
</I>&gt;<i> &gt; public int Y;
</I>&gt;<i> &gt; public Node(){}
</I>&gt;<i> &gt; public Node(int x,int y){
</I>&gt;<i> &gt; X=x;
</I>&gt;<i> &gt; Y=y;
</I>&gt;<i> &gt;
</I>&gt;<i> &gt; }
</I>&gt;<i> &gt;
</I>&gt;<i> &gt;}
</I>&gt;<i> &gt;
</I>&gt;<i> &gt;public class Arc{
</I>&gt;<i> &gt; public int Weight;
</I>&gt;<i> &gt; public Node StartAt;
</I>&gt;<i> &gt; public Node EndAt;
</I>&gt;<i> &gt; public Arc(){}
</I>&gt;<i> &gt; public Arc(Node n1, Node n2){
</I>&gt;<i> &gt; StartAt=n1;
</I>&gt;<i> &gt; EndAt = n2;
</I>&gt;<i> &gt; n1.OutGoingArcs.Add(this);
</I>&gt;<i> &gt; }
</I>&gt;<i> &gt;
</I>&gt;<i> &gt;}
</I>&gt;<i> &gt;
</I>&gt;<i> &gt;[Serializable, XmlInclude(typeof(Node))]
</I>&gt;<i> &gt;public class Graf{
</I>&gt;<i> &gt; public ArrayList Nodes;
</I>&gt;<i> &gt;
</I>&gt;<i> &gt; public Graf (){
</I>&gt;<i> &gt; Nodes = new ArrayList();
</I>&gt;<i> &gt; }
</I>&gt;<i> &gt;
</I>&gt;<i> &gt; public void Add(Node n){
</I>&gt;<i> &gt; Nodes.Add(n);
</I>&gt;<i> &gt; }
</I>&gt;<i> &gt;}
</I>&gt;<i> &gt;
</I>&gt;<i> &gt;public class M{
</I>&gt;<i> &gt;
</I>&gt;<i> &gt; public static void Main(){
</I>&gt;<i> &gt;
</I>&gt;<i> &gt; Graf g = new Graf();
</I>&gt;<i> &gt;
</I>&gt;<i> &gt; Node n1 = new Node(1,1);
</I>&gt;<i> &gt; Node n2 = new Node(2,2);
</I>&gt;<i> &gt;
</I>&gt;<i> &gt; Arc a1=new Arc(n1,n2);
</I>&gt;<i> &gt;
</I>&gt;<i> &gt;
</I>&gt;<i> &gt; g.Add(n1);
</I>&gt;<i> &gt; g.Add(n2);
</I>&gt;<i> &gt;
</I>&gt;<i> &gt; XmlSerializer seria = new XmlSerializer(typeof(Graf));
</I>&gt;<i> &gt; seria.Serialize(Console.Out,g);
</I>&gt;<i> &gt;
</I>&gt;<i> &gt;
</I>&gt;<i> &gt; }
</I>&gt;<i> &gt;
</I>&gt;<i> &gt;
</I>&gt;<i> &gt;}
</I>&gt;<i> &gt;
</I>&gt;<i> &gt;
</I>&gt;<i>
</I>&gt;<i> --
</I>&gt;<i>
</I>&gt;<i> _________________________
</I>&gt;<i> Phillip Neumann
</I>&gt;<i> <A HREF="mailto:phillip@sofsis.cl">phillip@sofsis.cl</A>
</I>&gt;<i> www.sofsis.cl
</I>&gt;<i>
</I>&gt;<i>
</I>&gt;<i>
</I>
</PRE>
<!--endarticle-->
<HR>
<P><UL>
<!--threads-->
<LI> Previous message: <A HREF="025879.html">[Mono-list] Serializing a Graph ..
</A></li>
<LI> Next message: <A HREF="025937.html">[Mono-list] Serializing a Graph ..
</A></li>
<LI> <B>Messages sorted by:</B>
<a href="date.html#25880">[ date ]</a>
<a href="thread.html#25880">[ thread ]</a>
<a href="subject.html#25880">[ subject ]</a>
<a href="author.html#25880">[ author ]</a>
</LI>
</UL>
</body></html>