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

XML Serialization and references?

Say I have a data class:

class Data {
public List<SubData> SubDataList1 { get ... set ... }
public List<Subdata> SubDataList2 { get ... set ... }
}

It's possible that a specific SubData instance is in SubDataList1 as
well as SubDataList2. When I serialize a Data instance, said SubData
instance is written to the XML file twice. After deserialization I have
two different objects instead of one that's in both lists.

How can I change this? I guess there'd have to be some support for
serialization of object references, where a reference to the same
SubData instance would be written as part of each list, with the SubData
instance itself being written separately. Or something. Any info on
this, please?
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 12 '05 #1
2 2254
"Oliver Sturm" <ol****@sturmnet.org> wrote in message news:ey****************@tk2msftngp13.phx.gbl...
When I serialize a Data instance, said SubData instance is written to the XML file twice. After deserialization I have two
different objects instead of one that's in both lists.

How can I change this?


What it sounds like you're looking for are "multi-reference accessors" (MRAs)
which is how SOAP rpc/encoded messages preserve the notion of an object's
identity.

The way MRAs work, and what gives them the ability to distinguish whether
a pair of objects have the same value or actually refer to the same instance,
is because the objects' values are each given a unique 'id' attribute and get
serialized outside of the list itself while the list contains references having an
'href' attribute into the set of serialized objects.

As an example, if I had a four-Person object array,

<SOAP-ENC:Array SOAP-ENC:arrayType="a1:Person[4]" xmlns:a1="http://www.example.com/">
<item href="#ref-1"/> <!-- Derek -->
<item href="#ref-2"/> <!-- Amy -->
<item href="#ref-3"/> <!-- Bob -->
<item href="#ref-2"/> <!-- Amy -->
</SOAP-ENC:Array>
<a1:Person id="ref-1" xmlns:a1="http://www.example.com/">
<Name id="ref-4">Derek</Name>
</a1:Person>
<a1:Person id="ref-2" xmlns:a1="http://www.example.com/">
<Name id="ref-5">Amy</Name>
</a1:Person>
<a1:Person id="ref-3" xmlns:a1="http://www.example.com/">
<Name id="ref-6">Bob</Name>
</a1:Person>

This would de-serialize into an array of four Person elements, but the 2nd and 4th
entries in that Person[] would both be 'Amy.' Not just a Person object with the
Name of 'Amy,' but the exact same object reference.

How do you get this in .NET?

Use the SoapFormatter, from the System.Runtime.Serialization.Formatters.Soap
namespace (add a reference to the .dll of the same name.) The above XML was
serialized with the following simple piece of code,

// Ensure your Person class is marked [Serializable] and has a default ctor.
Person p1 = new Person( "Derek");
Person p2 = new Person( "Amy");
Person p3 = new Person( "Bob");
Person[] people = new Person[] { p1, p2, p3, p2 };
// . . .
SoapFormatter formatter = new SoapFormatter( );
formatter.Serialize( fstream, people);

SoapFormatter is going to put all of this into a SOAP envelope, so if you don't
want that in your serialization then you can subclass a Stream class to intercept
and filter out any text written to it where the line of text starts with "<SOAP-ENV"
or "</SOAP-ENV".
Derek Harmon
Nov 12 '05 #2
Derek Harmon wrote:
What it sounds like you're looking for are "multi-reference accessors" (MRAs)
which is how SOAP rpc/encoded messages preserve the notion of an object's
identity.

The way MRAs work, and what gives them the ability to distinguish whether
a pair of objects have the same value or actually refer to the same instance,
is because the objects' values are each given a unique 'id' attribute and get
serialized outside of the list itself while the list contains references having an
'href' attribute into the set of serialized objects.


Thanks, that sounds like a good tip. I've actually used SOAP
serialization before, but I wasn't awary that this is an important
functional advantage over the "normal" XmlSerializer.

Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 12 '05 #3

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

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
1
by: MattBell | last post by:
I have a root node attribute which contains a name space declaration. I'd like to put a reference onto the front of it (ie: ns1:RootNode) where ns1 is defined as the namespace I'm using. Is...
3
by: Aaron Clamage | last post by:
Hi, I'm not sure that if this is the right forum, but any help would be greatly appreciated. I am porting some java serialization code to c# and I can't figure out the correct way to do it. ...
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: ChipOne | last post by:
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...
6
by: Uttam | last post by:
Hello, We are at a very crucial decision making stage to select between .Net and Java. Our requirement is to download a class at runtime on the client computer and execute it using remoting or...
0
by: umhlali | last post by:
I get the following exception when my VB.NET app calls a Java web service that returns an array of objects. The same call works for a single object though. So looks like there is no problem...
0
by: crazyone | last post by:
I've got a gaming framework i'm building and i want to save myself the trouble of reading and writting the complete game data to a custom file and load/save it to an XML file but i'm getting...
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; }
10
by: Atmapuri | last post by:
Hi! I would like to deserialize an object to which other unknown objects hold multiple references. Is it possible to deserialize the object without the need to destroy and recreate it? How? ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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: 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...

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.