XML Serialisation & Circular References

E

Earth Worm Jim

I have been able to get simple circular references to be serialized in xml
by using the ImportTypeMapping method on the SoapReflectionImporter class.

But I am unable to serialise circular references when the circular reference
is contained with in a collection class, specifically I am using a custom
ArrayList object. I keep getting a StackOverFlow Exception from the
XmlSerializer class when attempting the serialisation.

The classes are:

Class A,
Class B - this is derived from System.Collection.ArrayList
Class C.

Class A contains an instance of B
Class B contains multiple instances of C
Class C contains a reference to A

So in C# Code an example would be:


// Create parent object
A a = new A();

// Create container to hold children
B b = new B();

// Create children
C c1 = new C();
C c2 = new C();
C c3 = new C();

// Set the parent nodes for these children
c1.A = a;
c2.A = a;
c3.A = a;

// Assign the container to the parent
a.B = b;

// Add the children
b.Add(c1);
b.Add(c2);
b.Add(c3);

XmlTextWriter tw = new XMLTextWriter(Console.Out);
tw.WriteStartElement("Root");
XMLSerializer ser = new XmlSerializer(new
SoapReflectionImporter().ImportTypeMapping(typeof(A)));
ser.Serialize(tw, a);
tw.Close();


Cheers in advance

Ollie
 
N

Nicholas Paldino [.NET/C# MVP]

Jim,

From what I understand, you aren't going to be able to get circular
references to work using XmlSerialization. If you need an Xml format for
your object, then use the SoapFormatter, which will output XML, and also
handle the circular references correctly between objects. XML serialization
is just going to serialize the properties, not taking into account the fact
that the references might be the same.

Hope this helps.
 
E

Earth Worm Jim

Nick you are a STAR


Ollie

Nicholas Paldino said:
Jim,

From what I understand, you aren't going to be able to get circular
references to work using XmlSerialization. If you need an Xml format for
your object, then use the SoapFormatter, which will output XML, and also
handle the circular references correctly between objects. XML serialization
is just going to serialize the properties, not taking into account the fact
that the references might be the same.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Earth Worm Jim said:
I have been able to get simple circular references to be serialized in xml
by using the ImportTypeMapping method on the SoapReflectionImporter class.

But I am unable to serialise circular references when the circular reference
is contained with in a collection class, specifically I am using a custom
ArrayList object. I keep getting a StackOverFlow Exception from the
XmlSerializer class when attempting the serialisation.

The classes are:

Class A,
Class B - this is derived from System.Collection.ArrayList
Class C.

Class A contains an instance of B
Class B contains multiple instances of C
Class C contains a reference to A

So in C# Code an example would be:


// Create parent object
A a = new A();

// Create container to hold children
B b = new B();

// Create children
C c1 = new C();
C c2 = new C();
C c3 = new C();

// Set the parent nodes for these children
c1.A = a;
c2.A = a;
c3.A = a;

// Assign the container to the parent
a.B = b;

// Add the children
b.Add(c1);
b.Add(c2);
b.Add(c3);

XmlTextWriter tw = new XMLTextWriter(Console.Out);
tw.WriteStartElement("Root");
XMLSerializer ser = new XmlSerializer(new
SoapReflectionImporter().ImportTypeMapping(typeof(A)));
ser.Serialize(tw, a);
tw.Close();


Cheers in advance

Ollie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top