By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,165 Members | 1,916 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,165 IT Pros & Developers. It's quick & easy.

XML Serialisation & Circular References

P: n/a
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



Nov 12 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Earth Worm Jim" <sdd> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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




Nov 12 '05 #2

P: n/a
Nick you are a STAR
Ollie

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:#B*************@TK2MSFTNGP12.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Earth Worm Jim" <sdd> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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





Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.