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

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 1496
<cj*******@gmail.com> wrote in message news:11**********************@o13g2000cwo.googlegr oups.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.Formatters.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.ToString( ));
}
}

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

SoapFormatter sf = new SoapFormatter( );
sf.Serialize( Console.OpenStandardOutput( ), 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
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...
0
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...
1
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...
2
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: +...
4
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...
2
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...
2
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...
0
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...
47
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...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.