473,796 Members | 2,429 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Serializing objects that reference other objects

I have a couple of C# objects like this:

class Foo {

}

class Bar {

Foo m_foo;
Foo foo {
get {
return m_foo;
}
set {
m_foo = value;
}
}
}

What I get when these are serialized is a Foo element nested in a Bar
element. Instead I want it to somehow reference it. Is this possible?

Nov 12 '05 #1
1 1514
<cj*******@gmai l.com> wrote in message news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
What I get when these are serialized is a Foo element nested in a Bar
element. Instead I want it to somehow reference it. Is this possible?


You can mark these objects [Serializable] and pump them through a
SOAP Formatter. SOAP encoding allows for "multi-reference
accessors" which is just a fancy way of saying references from
one or more objects to a common [shared] object. MRA is
useful to preserve the identity of the common object (i.e., if each
referring object contained a verbatim nested copy of the common
object, you couldn't say for certain that the common object
was truly the identical object reference across all referrers or
simply an instance that was value-equivalent).

Here is an example,

- - - MultiRefFoo.cs
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Runtime. Serialization;
using System.Runtime. Serialization.F ormatters.Soap;

[Serializable]
public class Foo {
private string property;
public Foo( ) {
this.property = "";
}
public string PropertyName {
get {
return this.property;
}
set {
this.property = value;
}
}
public override string ToString( ) {
return string.Format( "Foo[ Property[ {0} ] ]",
this.property);
}
}

[Serializable]
public class Bar {
public Bar( ) {
this.m_foo = new Foo( );
}
private Foo m_foo;
public Foo Foo {
get {
return this.m_foo;
}
set {
this.m_foo = value;
}
}
public override string ToString( ) {
return string.Format( "Bar [ {0} ]",
this.m_foo.ToSt ring( ));
}
}

public class TestApp {
public static void Main( ) {
Bar bar = new Bar( );
bar.Foo = new Foo( );
bar.Foo.Propert yName = "C.J.";

SoapFormatter sf = new SoapFormatter( );
sf.Serialize( Console.OpenSta ndardOutput( ), bar);
}
}
- - -

Observe that in the resulting XML output, that there are two children of
the SOAP body: Bar and Foo. Notice that the Bar element contains a
reference to the Foo element (by the Foo element's 'id'). If you had
serialized multiple Bar objects, which all referred to the same instance
of Foo, the Foo element you see here would only be serialized once.
Derek Harmon
Nov 12 '05 #2

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

Similar topics

1
3844
by: Martin Zeigler | last post by:
I have been using Weblogic 5.1 for a couple of years. I currently pass an object as a parameter to my ejbCreate method. The data in the object could be modified by my bean and the caller needs to see these changes. This worked fine in 5.1, but it does not work since I have upgraded to Weblogic 7.0. The changes to the object passed as a parameter does not make it back to the caller on the remote interface. I assume the object is not...
0
2825
by: Tom Kent | last post by:
I have an array list of objects that I want to serialize into an XML file. I have tried this using the following code, but it gives me an error message An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dl Additional information: There was an error generating the XML document I'm not sure if this is the way that I should go about serializing an array of objects like this, but I would like to get them...
1
2088
by: Ivo Bronsveld | last post by:
All, I have quite a challenging task ahead of me. I need to write an object model (for code access) based on a schema, which cannot be made into a dataset because of it's complexity. So I created a couple of objects and serializing it into XML based upon the schema works perfectly. The XML / Schema looks something like this:
2
3524
by: Tobias Zimmergren | last post by:
Hi, just wondering what serializing really is, and howto use it? Thanks. Tobias __________________________________________________________________ Tobias ICQ#: 55986339 Current ICQ status: + More ways to contact me __________________________________________________________________
4
3621
by: Dave Veeneman | last post by:
When does serializing objects make more sense than persisting them to a database? I'm new to object serialization, and I'm trying to get a feel for when to use it. Here is an example: I'm writing an accounting application. I have a chart of accounts in the form of a containment hierarchy. A GeneralLedger contains a number of Accounts, and each of these Accounts can contain a Aubledger, which contains its own Accounts, and so on. The...
2
1208
by: Jacob | last post by:
I've had great success with the XmlSerializer but ran into a new problem. Say I have "classA" with a reference to "myObject" and "classB" with a reference to "myObject" ... it appears that the one "myObject" gets serialized as two separate objects. And consequently, when you deserialized "classA" and "classB" the two "myObjects" are not equal. How can I serialize a class so that any references to the same object, are deserialized the...
2
1619
by: Dennis | last post by:
Does anyone know why this code gives an error: <serializeable> public structure TestSerialize anumber as integer astring as string end strucuture Dim t As TestSerialize Dim formatter As New BinaryFormatter Dim s As New MemoryStream
0
1349
by: Leonid Levin | last post by:
Hi, I am builidng an ASP.Net web application consuming a .Net web service. The web service requires me to log in and keeps my login status in its own session state. I don't want to log in every time I call the web service, so I create an instance of the web service proxy object in my application, log in and set the object into my own session state. That has worked fine. Now the requirements changed, and my web application needs to be able...
47
3121
by: Max | last post by:
Due to the behaviour of a particular COM object, I need to ensure that a request for a particular ASP page is finalized before another request for the page is processed. Does IIS have a way to ensure that subsequent requests will be queued until the current request is completed? If not, can IIS be configured to use seperate processes to satisfy requests for a nominated ASP page? Thanks in advance.
0
9528
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10456
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10012
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6788
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5442
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4118
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 we have to send another system
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.