473,468 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 12 '05 #1
2 7942
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
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...
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...
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...
2
by: Dansk | last post by:
Hi all, I am currently writing some code that explores assemblies dependencies. I start loading the first assembly with Assmebly.LoadFrom which gives me an Assembly instance. Then, I...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.