473,411 Members | 1,982 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,411 software developers and data experts.

XML Serialization and Circular References

I have a Widget Class with a Parent and Child property of type Widget.

When you populate a set of Widgets, you set references to the Parent and Child.

I was using binary serialization, which works fine, but XML serialization does not. I believe this is a bug, as the .NET documentation states:
"When the class is serialized, the serialization engine keeps track of all referenced objects already serialized to ensure that the same object is not serialized more than once."
.....and...
"The serialization architecture provided with the .NET Framework correctly handles object graphs and circular references automatically. "

Code sample is below. To toggle the test cases, simply delete ONE of the / in the //*/ comment under "test one". If you remove the two lines that set up the parents (forming the parent references) the xml processing works fine. Otherwise the error returned is "There was an error generating the XML document.". Any ideas?

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;

namespace SerializationBugTest
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Widget myWidget = new Widget();
myWidget.Name = "Top";

Widget myChildWidget = new Widget();
myChildWidget.Name = "Child";

Widget myGrandchild = new Widget();
myGrandchild.Name = "Grandchild";

myWidget.Child = myChildWidget;
myChildWidget.Child = myGrandchild;
// comment out these lines to make XML serialization work
myChildWidget.Parent = myWidget;
myGrandchild.Parent = myChildWidget;

Console.WriteLine(myWidget.Name);
Console.WriteLine(myGrandchild.Name);
Console.WriteLine(myGrandchild.Parent.Parent.Name) ;
Console.WriteLine(myWidget.Child.Parent.Child.Chil d.Name);

// test one
//*
Stream myStream ;
myStream = File.OpenWrite("Temp.bin");
if (myStream != null)
{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(myStream, myWidget);
myStream.Close();
}

// test two
/*/

myWidget.Child = myChildWidget;
myChildWidget.Child = myGrandchild;
myChildWidget.Parent = myWidget;
myGrandchild.Parent = myChildWidget;

XmlSerializer x = new XmlSerializer(typeof(Widget));
TextWriter writer = new StreamWriter("Temp.xml");
x.Serialize(writer, myWidget);
writer.Close();
//*/

}
}
}

Nov 15 '05 #1
0 1910

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

Similar topics

3
by: Franz | last post by:
Let's see the code clip first. :-) <?xml version="1.0"?> <A> <B Test="xxxxxxx"/> <B Test="xxxxxxx"/> <B Test="xxxxxxx"/> </A>
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...
0
by: Jean Bon | last post by:
hello I have an application which manages a tree of objects. each object in the tree have a lot of references on other objects in the tree. in the application, I serialize a lot this tree using...
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...
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)...
0
by: David Levine | last post by:
I want to define a web method that returns an object that contains a member field that is an array of objects of the same type and another field that is the parent of the current object. The class...
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...
2
by: =?ISO-8859-1?Q?=22Andr=E9s_G=2E_Aragoneses_=5B_kno | last post by:
Hello. I have this kind of object: class classA { int x; classA z; public int X { get { return this.x; }
1
by: kikisan | last post by:
I am developing a windows service which utilizes the following classes: interface IPersistable; abstract class PersistableObject : IPersistable;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.