473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

XML Serialisation & Circular References

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 15 '05 #1
2 1993
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 15 '05 #2
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 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Eric | last post by:
Hi, I'm used to C/C++ where if you "new" something you really need to "delete" it later. Is that also true in javascript? if i do "mydate = new date();" in a function and dont "delete mydate" when...
16
by: Kiuhnm | last post by:
Is there an elegant way to deal with semi-circular definitions? Semi-circular definition: A { B }; B { *A }; Circular reference: A { *B }; B { *A }; The problems arise when there are more...
2
by: Earth Worm Jim | last post by:
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...
8
by: Eric Eggermann | last post by:
I'm having a problem with really large file sizes when serializing the classes that describe my little document. There are some circular references which result in the same object getting written...
12
by: Frank Rizzo | last post by:
I have a circular reference between 2 classes in the same project (i.e. each class refers to the other). The app runs fine and I am seeing no issues, which kind of surprised me. Are there any...
5
by: Gos | last post by:
Hi, It is known that .NET does not allow us to add circular references. Is there a way to workaround this problem by late-binding the objects at run time? Will this create any other problems? ...
6
by: Stephen Robertson | last post by:
We are currently in a dead end with a circular reference issue using vb.net, and are hoping someone might help us resolve it. Idea... We have frmmain calling frmperson (dim f as new frmperson)...
2
by: Lapu-Lapu | last post by:
I have authored a web service using ASP 2.0. The web services return objects that use generics and that also contain circular references. Programmatically, everything works well, as long as you...
5
by: Madhur | last post by:
Hello If I define two classes in the same cs file. And in each class, I define the object of other class as a member. Can anyone explain me how .NET or its compiler will resolve this kind of...
3
by: =?Utf-8?B?UGF1bCBIYWxl?= | last post by:
Moving all User Controls to a single directory has solved my problem - Thanks Eliyahu. That said, I still got one Circular ref error yesterday, rebuilt again and the build was fine? Far far...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.