Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
microsoft.public.dotnet.framework.sdk
Serializing object graph with xmlSerializer
David Boaz
10/6/2002 9:13:00 AM
I have a class with two fields that refers to the same type.
when this refrences referes to the same instance, the referenced object is
serialized twice. when I deserialize the object, It creates two diffrent
object. how can I deserialze the object when the fields refers to the same
instance.
the example below shows the problem. note that before serrializing
x1.a==x1.b. but after deserializing x1.a != x1.b.
public class Test
{
public string name;
public Test a,b;
public Test(){}
public Test(string name)
{
this.name=name;
}
public static void Main()
{
Test x1=new Test("x1"),x2=new Test("x2");
x1.a=x2;
x1.b=x2;
serializeObject(x1,"simple.xml");
x1=deserializeObject("simple.xml");
x1.b.name="aaa";
Console.WriteLine("x1.a.name is:"+x1.a.name);
Console.WriteLine("x1.b.name is:"+x1.b.name);
}
private static void serializeObject(Test x,string filename)
{
XmlSerializer serializer = new XmlSerializer(typeof(Test));
Stream writer = new FileStream(filename, FileMode.Create);
serializer.Serialize(writer, x);
writer.Close();
}
private static Test deserializeObject(string filename)
{
XmlSerializer serializer = new XmlSerializer(typeof(Test));
Stream reader= new FileStream(filename,FileMode.Open);
return (Test) serializer.Deserialize(reader);
}
}
/*THE PROGRAM OUTPUT*/
x1.a.name is:x2
x1.b.name is:aaa
/*The serialized XML*/
<Test xmlns:xsd="
http://www.w3.org/2001/XMLSc...
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance...
<name>x1</name>
- <a>
<name>x2</name>
</a>
- <b>
<name>x2</name>
</b>
</Test>
thanks David.
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Serializing object graph with xmlSerializer
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password