Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / May 2008

Tip: Looking for answers? Try searching our database.

How do I serialize element references as IDREFs?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Niklas Deutschmann - 09 May 2008 09:05 GMT
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 - 09 May 2008 12:01 GMT
> 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

Signature

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

Niklas Deutschmann - 13 May 2008 09:17 GMT
Hi Martin,

> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.