Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

How do I serialize element references as IDREFs?

Question posted by: =?Utf-8?B?TmlrbGFzIERldXRzY2htYW5u?= (Guest) on June 27th, 2008 07:20 PM
Hi,

Is it possible in .NET XML serialization to store element references in
attributes, maybe using IDs and IDREF? For element references, I always get a
copy of the element instead of a reference in the XML code.

That's what I want:

<FlightData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="htt
p://www.w3.org/2001/XMLSchema" xmlns="http://demo.ns/flightdata">
<airport code="FRA" cityName="Frankfurt" />
<airport code="SFO" cityName="San Francisco" />
<flight flightNumber="UA2344" from="#FRA" to="#SFO" />
</FlightData>

That's what I get instead:

<FlightData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="htt
p://www.w3.org/2001/XMLSchema" xmlns="http://demo.ns/flightdata">
<airport code="FRA" cityName="Frankfurt" />
<airport code="SFO" cityName="San Francisco" />
<flight flightNumber="UA2344">
<from code="FRA" cityName="Frankfurt" />
<to code="SFO" cityName="San Francisco" />
</flight>
</FlightData>

Is there any combination of serialization attributes that can accomplish this,
or do I have to use a custom XML serializer? BTW, I'm using Visual Studio
2008.

Regards,
Niklas

Martin Honnen's Avatar
Martin Honnen
Guest
n/a Posts
June 27th, 2008
07:20 PM
#2

Re: How do I serialize element references as IDREFs?
Niklas Deutschmann wrote:
Quote:
Is there any combination of serialization attributes that can accomplish this,
or do I have to use a custom XML serializer? BTW, I'm using Visual Studio
2008.


SOAP serialization does allow references I think. It will however not
have the format you describe.

The new DataContractSerializer in .NET 3.5 also allows references to
objects. Here is an example from MSDN:
------------------------- quote -------------------------------------
For these reasons, some DataContractSerializer constructor overloads
have a preserveObjectReferences parameter (the default is false). When
this parameter is set to true, a special method of encoding object
references, which only WCF understands, is used. When set to true, the
XML code example now resembles the following.


<PurchaseOrder ser:id="1">
<billTo ser:id="2"><street ser:id="3">123 Main St.</street></billTo>
<shipTo ser:ref="2"/>
</PurchaseOrder>

----------------------- quote ----------------------------------------

The documentation is here:
http://msdn.microsoft.com/en-us/library/ms731073.aspx


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

=?Utf-8?B?TmlrbGFzIERldXRzY2htYW5u?='s Avatar
=?Utf-8?B?TmlrbGFzIERldXRzY2htYW5u?=
Guest
n/a Posts
June 27th, 2008
07:20 PM
#3

Re: How do I serialize element references as IDREFs?
Hi Martin,
Quote:
The new DataContractSerializer in .NET 3.5 also allows references to
objects. Here is an example from MSDN


So, if I'm understanding it correctly, with this kind of serialization, I
cannot choose the attributes for ID and reference myself? I would like to say
that "code" is the identifying attribute of "Airport", for instance, and that
any reference to an "Aiport" should be encoded as an ID reference with the
"code" attribute of this "Aiport".

Regards,
Niklas

 
Not the answer you were looking for? Post your question . . .
190,475 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors