Conditional serialization of an element?

Discussion in 'ASP .Net Web Services' started by MikeL, Feb 17, 2005.

  1. MikeL

    MikeL Guest

    Hello.

    I've created a web service based on a message contract with a business
    partner.

    In the schema some elements are optional.

    How do I prevent an optional element from being serialized into the response
    XML (I don't want it to show up) if the value of the element is a particular
    value?

    Thanks in advance,

    Mike
     
    MikeL, Feb 17, 2005
    #1
    1. Advertising

  2. MikeL

    Dan Rogers Guest

    For reference types (strings, objects, etc), just make the value null -
    null references aren't serialized.
    For value types, you have to add a "friend" to your code... eg

    public int myFoo;
    public bool myFooSpecified;

    To supress myFoo (which always has a value, which is by default false)
    serialization, set myFooSpecified to false (the default). To make it get
    serialized, set myFooSpecified to true.

    No matter what your value type field (or property) is, a boolean friend
    with the same first name and Specified at the end will do the trick.

    --------------------
    >From: "MikeL" <>
    >Subject: Conditional serialization of an element?
    >Date: Thu, 17 Feb 2005 15:10:35 -0500
    >Lines: 16
    >X-Priority: 3
    >X-MSMail-Priority: Normal
    >X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
    >X-RFC2646: Format=Flowed; Original
    >X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
    >Message-ID: <#$Qx#>
    >Newsgroups: microsoft.public.dotnet.framework.aspnet.webservices
    >NNTP-Posting-Host: 208.28.116.33
    >Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
    >Xref: TK2MSFTNGXA01.phx.gbl

    microsoft.public.dotnet.framework.aspnet.webservices:5608
    >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webservices
    >
    >Hello.
    >
    >I've created a web service based on a message contract with a business
    >partner.
    >
    >In the schema some elements are optional.
    >
    >How do I prevent an optional element from being serialized into the

    response
    >XML (I don't want it to show up) if the value of the element is a

    particular
    >value?
    >
    >Thanks in advance,
    >
    >Mike
    >
    >
    >
     
    Dan Rogers, Mar 8, 2005
    #2
    1. Advertising

  3. MikeL

    MikeL Guest

    Thanks for responding, Dan.

    It didn't take me long until I figured that one out.

    Thanks again,

    Mike

    "Dan Rogers" <> wrote in message
    news:d$...
    > For reference types (strings, objects, etc), just make the value null -
    > null references aren't serialized.
    > For value types, you have to add a "friend" to your code... eg
    >
    > public int myFoo;
    > public bool myFooSpecified;
    >
    > To supress myFoo (which always has a value, which is by default false)
    > serialization, set myFooSpecified to false (the default). To make it get
    > serialized, set myFooSpecified to true.
    >
    > No matter what your value type field (or property) is, a boolean friend
    > with the same first name and Specified at the end will do the trick.
    >
    > --------------------
    >>From: "MikeL" <>
    >>Subject: Conditional serialization of an element?
    >>Date: Thu, 17 Feb 2005 15:10:35 -0500
    >>Lines: 16
    >>X-Priority: 3
    >>X-MSMail-Priority: Normal
    >>X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
    >>X-RFC2646: Format=Flowed; Original
    >>X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
    >>Message-ID: <#$Qx#>
    >>Newsgroups: microsoft.public.dotnet.framework.aspnet.webservices
    >>NNTP-Posting-Host: 208.28.116.33
    >>Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
    >>Xref: TK2MSFTNGXA01.phx.gbl

    > microsoft.public.dotnet.framework.aspnet.webservices:5608
    >>X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webservices
    >>
    >>Hello.
    >>
    >>I've created a web service based on a message contract with a business
    >>partner.
    >>
    >>In the schema some elements are optional.
    >>
    >>How do I prevent an optional element from being serialized into the

    > response
    >>XML (I don't want it to show up) if the value of the element is a

    > particular
    >>value?
    >>
    >>Thanks in advance,
    >>
    >>Mike
    >>
    >>
    >>

    >
     
    MikeL, Mar 14, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    2,313
    deadsea
    Jan 2, 2005
  2. Alec S.
    Replies:
    10
    Views:
    10,328
    Alec S.
    Apr 16, 2005
  3. Replies:
    3
    Views:
    1,087
  4. Dimitri Ognibene
    Replies:
    4
    Views:
    824
    Dimitri Ognibene
    Sep 2, 2006
  5. Ramunas Urbonas
    Replies:
    1
    Views:
    427
    Dino Chiesa [Microsoft]
    Jul 27, 2004
Loading...

Share This Page