How do I serialize element references as IDREFs?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?TmlrbGFzIERldXRzY2htYW5u?=

    How do I serialize element references as IDREFs?

    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="Frank furt" />
    <airport code="SFO" cityName="San Francisco" />
    <flight flightNumber="U A2344" 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="Frank furt" />
    <airport code="SFO" cityName="San Francisco" />
    <flight flightNumber="U A2344">
    <from code="FRA" cityName="Frank furt" />
    <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

    #2
    Re: How do I serialize element references as IDREFs?

    Niklas Deutschmann wrote:
    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 DataContractSer ializer in .NET 3.5 also allows references to
    objects. Here is an example from MSDN:
    ------------------------- quote -------------------------------------
    For these reasons, some DataContractSer ializer constructor overloads
    have a preserveObjectR eferences 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.


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

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

    The documentation is here:
    Learn about the WCF serialization engine, which translates between .NET Framework objects and XML, in both directions.



    --

    Martin Honnen --- MVP XML

    Comment

    • =?Utf-8?B?TmlrbGFzIERldXRzY2htYW5u?=

      #3
      Re: How do I serialize element references as IDREFs?

      Hi Martin,
      The new DataContractSer ializer 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

      Comment

      Working...