Xml serialization

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hello!

What does it mean what it says that you cannot serialize object graphs; you
can use XML serialization only on objects ?

//Tony
 
Tony said:
What does it mean what it says that you cannot serialize object graphs; you
can use XML serialization only on objects ?

I think that an object graph can contain cycles and that XmlSerializer
cannot deal with cycles.
 
Martin Honnen said:
I think that an object graph can contain cycles and that XmlSerializer
cannot deal with cycles.

Can you give a simple example ?

//Tony
 
What does what mean? In the above sentence, do what does "it" refer?
Can you give a simple example ?

class A
{
public B B { get; set; }
}

class B
{
public A A { get; set; }
}

class C
{
public void SerializeA()
{
A a = new A();
B b = new B();

a.B = b;
b.A = a;

// Serialize here (or try to!)
}
}

That said, not all object graphs have cycles, and AFAIK the .NET
serialization stuff doesn't have problems with acyclic graphics. Is
this question yet another from that awful study materials you've been using?

Pete
 
Can you give a simple example ?

using System;
using System.Xml.Serialization;

namespace E
{
public class C1
{
public C2 Other { get; set; }
}
public class C2
{
public C1 Other { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
C1 o1 = new C1();
C2 o2 = new C2();
o1.Other = o2;
o2.Other = o1;
XmlSerializer ser = new XmlSerializer(typeof(C1));
ser.Serialize(Console.Out, o1);
Console.ReadKey();
}
}
}

gives:

System.InvalidOperationException: There was an error generating the XML
document. ---> System.InvalidOperationException: A circular reference
was detected while serializing an object of type E.C1.
at System.Xml.Serialization.XmlSerializer.Serialize
at System.Xml.Serialization.XmlSerializer.Serialize
at System.Xml.Serialization.XmlSerializer.Serialize
at E.Program.Main

Arne
 
That said, not all object graphs have cycles, and AFAIK the .NET
serialization stuff doesn't have problems with acyclic graphics.

It may also be relevant to note that the "real" serialization
(BinaryFormatter and SoapFormatter) can handle cycles - it is only
XmlSerializer that has the problem.

Arne
 
Back
Top