How to tell if a string element is null or really null?

Discussion in 'ASP .Net' started by Jiho Han, Jul 24, 2006.

  1. Jiho Han

    Jiho Han Guest

    Here's the issue.

    You have a class,

    Class Person
    {
    public int id;
    public string firstname;
    public string lastname;
    }

    when you expose it via webservice,

    [WebMethod]
    public void UpdatePerson(Person)
    {
    ...
    }

    How do I know whether a field value has been specified or not? For value
    types, there is that matching XXXXSpecified buddy field, but for string type,
    there doesn't seem to be one.
    So if incoming Xml looks something like this:
    <Person>
    <firstname>Jiho</firstname>
    </Person>

    when deserialized into a Person object, lastname field will contain null,
    which doesn't tell me whether the field is simply missing (thus safely ignored)
    or lastname field should be set to null (in the database, for example).

    Any idea how to go about this?
    Thanks in advance.


    Jiho Han
    Senior Software Engineer
    Infinity Info Systems
    The Sales Technology Experts
    Tel: 212.563.4400 x216
    Fax: 212.760.0540

    www.infinityinfo.com
    Jiho Han, Jul 24, 2006
    #1
    1. Advertising

  2. It is as simple as this,

    if( IsNullOrEmpty(person.lastname) )
    // Echo Is null
    else
    // Echo Is not null

    That is only valid for string types. For other kind of reference type, you
    should check if it is equal to null.

    if( person.lastname == null)
    // Echo Is null
    else
    // Echo Is not null

    "Jiho Han" <> wrote in message
    news:...
    > Here's the issue.
    >
    > You have a class,
    >
    > Class Person
    > {
    > public int id;
    > public string firstname;
    > public string lastname;
    > }
    >
    > when you expose it via webservice,
    >
    > [WebMethod]
    > public void UpdatePerson(Person)
    > {
    > ...
    > }
    >
    > How do I know whether a field value has been specified or not? For value
    > types, there is that matching XXXXSpecified buddy field, but for string
    > type, there doesn't seem to be one.
    > So if incoming Xml looks something like this:
    > <Person>
    > <firstname>Jiho</firstname>
    > </Person>
    >
    > when deserialized into a Person object, lastname field will contain null,
    > which doesn't tell me whether the field is simply missing (thus safely
    > ignored) or lastname field should be set to null (in the database, for
    > example).
    >
    > Any idea how to go about this?
    > Thanks in advance.
    >
    >
    > Jiho Han
    > Senior Software Engineer
    > Infinity Info Systems
    > The Sales Technology Experts
    > Tel: 212.563.4400 x216
    > Fax: 212.760.0540
    >
    > www.infinityinfo.com
    >
    >
    Pablo Cibraro [MVP], Jul 24, 2006
    #2
    1. Advertising

  3. Jiho Han

    Jiho Han Guest

    Pablo,

    Perhaps, it is more of a design question...

    If you want a field to be set to null (think database), how would you specify
    that from the client side?
    If you don't include the field, it's null on the server. If you set it to
    null, it's still null on the server.
    My point is that there seems to be no way of knowing whether the client intended
    a field to be set to null vs. skip the field for processing becasue it's
    missing from the xml.
    i.e.)
    <Person>
    <Lastname>Han</Lastname>
    <Firstname/>
    </Person>

    and

    <Person>
    <Lastname>Han</Lastname>
    </Person>

    deserializes into an identical object state.

    > It is as simple as this,
    >
    > if( IsNullOrEmpty(person.lastname) )
    > // Echo Is null
    > else
    > // Echo Is not null
    > That is only valid for string types. For other kind of reference type,
    > you should check if it is equal to null.
    >
    > if( person.lastname == null)
    > // Echo Is null
    > else
    > // Echo Is not null
    > "Jiho Han" <> wrote in message
    > news:...
    >
    >> Here's the issue.
    >>
    >> You have a class,
    >>
    >> Class Person
    >> {
    >> public int id;
    >> public string firstname;
    >> public string lastname;
    >> }
    >> when you expose it via webservice,
    >>
    >> [WebMethod]
    >> public void UpdatePerson(Person)
    >> {
    >> ...
    >> }
    >> How do I know whether a field value has been specified or not? For
    >> value
    >> types, there is that matching XXXXSpecified buddy field, but for
    >> string
    >> type, there doesn't seem to be one.
    >> So if incoming Xml looks something like this:
    >> <Person>
    >> <firstname>Jiho</firstname>
    >> </Person>
    >> when deserialized into a Person object, lastname field will contain
    >> null, which doesn't tell me whether the field is simply missing (thus
    >> safely ignored) or lastname field should be set to null (in the
    >> database, for example).
    >>
    >> Any idea how to go about this?
    >> Thanks in advance.
    >> Jiho Han
    >> Senior Software Engineer
    >> Infinity Info Systems
    >> The Sales Technology Experts
    >> Tel: 212.563.4400 x216
    >> Fax: 212.760.0540
    >>
    >> www.infinityinfo.com
    Jiho Han, Jul 24, 2006
    #3
  4. An XML document, and in particular, a SOAP document, is not quite as simple
    as you seem to think. It can indicate whether the value is null or not.
    Example:

    <SomeObject xsi:nil="true" />

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    Professional Chicken Salad Alchemist

    Sequence, Selection, Iteration.

    "Jiho Han" <> wrote in message
    news:...
    > Pablo,
    >
    > Perhaps, it is more of a design question...
    >
    > If you want a field to be set to null (think database), how would you
    > specify that from the client side?
    > If you don't include the field, it's null on the server. If you set it to
    > null, it's still null on the server.
    > My point is that there seems to be no way of knowing whether the client
    > intended a field to be set to null vs. skip the field for processing
    > becasue it's missing from the xml.
    > i.e.)
    > <Person>
    > <Lastname>Han</Lastname>
    > <Firstname/>
    > </Person>
    >
    > and
    > <Person>
    > <Lastname>Han</Lastname>
    > </Person>
    >
    > deserializes into an identical object state.
    >
    >> It is as simple as this,
    >>
    >> if( IsNullOrEmpty(person.lastname) )
    >> // Echo Is null
    >> else
    >> // Echo Is not null
    >> That is only valid for string types. For other kind of reference type,
    >> you should check if it is equal to null.
    >>
    >> if( person.lastname == null)
    >> // Echo Is null
    >> else
    >> // Echo Is not null
    >> "Jiho Han" <> wrote in message
    >> news:...
    >>
    >>> Here's the issue.
    >>>
    >>> You have a class,
    >>>
    >>> Class Person
    >>> {
    >>> public int id;
    >>> public string firstname;
    >>> public string lastname;
    >>> }
    >>> when you expose it via webservice,
    >>>
    >>> [WebMethod]
    >>> public void UpdatePerson(Person)
    >>> {
    >>> ...
    >>> }
    >>> How do I know whether a field value has been specified or not? For
    >>> value
    >>> types, there is that matching XXXXSpecified buddy field, but for
    >>> string
    >>> type, there doesn't seem to be one.
    >>> So if incoming Xml looks something like this:
    >>> <Person>
    >>> <firstname>Jiho</firstname>
    >>> </Person>
    >>> when deserialized into a Person object, lastname field will contain
    >>> null, which doesn't tell me whether the field is simply missing (thus
    >>> safely ignored) or lastname field should be set to null (in the
    >>> database, for example).
    >>>
    >>> Any idea how to go about this?
    >>> Thanks in advance.
    >>> Jiho Han
    >>> Senior Software Engineer
    >>> Infinity Info Systems
    >>> The Sales Technology Experts
    >>> Tel: 212.563.4400 x216
    >>> Fax: 212.760.0540
    >>>
    >>> www.infinityinfo.com

    >
    >
    Kevin Spencer, Jul 25, 2006
    #4
  5. Jiho Han

    Jiho Han Guest

    Kevin,

    I understand that the xml received by the soap endpoint is different. However,
    when the xml document is deserialized into an object, there is no difference
    - or rather I don't know of a way - between:

    <Person>
    <Lastname xsi:nil = "true"/>
    </Person>

    vs.

    <Person>
    </Person>

    When it gets deserialized into an object,

    Person person = <from soap response>
    person.Lastname == null // true for both!

    If there is a way to tell the difference, that'd be great. Is there some
    kind of a hook into the soap deserialization scheme?

    > An XML document, and in particular, a SOAP document, is not quite as
    > simple as you seem to think. It can indicate whether the value is null
    > or not. Example:
    >
    > <SomeObject xsi:nil="true" />
    >
    > Kevin Spencer
    > Microsoft MVP
    > Professional Chicken Salad Alchemist
    > Sequence, Selection, Iteration.
    >
    > "Jiho Han" <> wrote in message
    > news:...
    >
    >> Pablo,
    >>
    >> Perhaps, it is more of a design question...
    >>
    >> If you want a field to be set to null (think database), how would you
    >> specify that from the client side?
    >> If you don't include the field, it's null on the server. If you set
    >> it to
    >> null, it's still null on the server.
    >> My point is that there seems to be no way of knowing whether the
    >> client
    >> intended a field to be set to null vs. skip the field for processing
    >> becasue it's missing from the xml.
    >> i.e.)
    >> <Person>
    >> <Lastname>Han</Lastname>
    >> <Firstname/>
    >> </Person>
    >> and
    >> <Person>
    >> <Lastname>Han</Lastname>
    >> </Person>
    >> deserializes into an identical object state.
    >>
    >>> It is as simple as this,
    >>>
    >>> if( IsNullOrEmpty(person.lastname) )
    >>> // Echo Is null
    >>> else
    >>> // Echo Is not null
    >>> That is only valid for string types. For other kind of reference
    >>> type,
    >>> you should check if it is equal to null.
    >>> if( person.lastname == null)
    >>> // Echo Is null
    >>> else
    >>> // Echo Is not null
    >>> "Jiho Han" <> wrote in message
    >>> news:...
    >>>> Here's the issue.
    >>>>
    >>>> You have a class,
    >>>>
    >>>> Class Person
    >>>> {
    >>>> public int id;
    >>>> public string firstname;
    >>>> public string lastname;
    >>>> }
    >>>> when you expose it via webservice,
    >>>> [WebMethod]
    >>>> public void UpdatePerson(Person)
    >>>> {
    >>>> ...
    >>>> }
    >>>> How do I know whether a field value has been specified or not? For
    >>>> value
    >>>> types, there is that matching XXXXSpecified buddy field, but for
    >>>> string
    >>>> type, there doesn't seem to be one.
    >>>> So if incoming Xml looks something like this:
    >>>> <Person>
    >>>> <firstname>Jiho</firstname>
    >>>> </Person>
    >>>> when deserialized into a Person object, lastname field will contain
    >>>> null, which doesn't tell me whether the field is simply missing
    >>>> (thus
    >>>> safely ignored) or lastname field should be set to null (in the
    >>>> database, for example).
    >>>> Any idea how to go about this?
    >>>> Thanks in advance.
    >>>> Jiho Han
    >>>> Senior Software Engineer
    >>>> Infinity Info Systems
    >>>> The Sales Technology Experts
    >>>> Tel: 212.563.4400 x216
    >>>> Fax: 212.760.0540
    >>>>
    >>>> www.infinityinfo.com
    Jiho Han, Jul 25, 2006
    #5
  6. I haven't personally encountered this situation. However, I think that
    applying some custom formatting to the SOAP XSD might do the trick. See
    http://msdn2.microsoft.com/en-us/library/k1y9z356.aspx

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    Professional Chicken Salad Alchemist

    Sequence, Selection, Iteration.


    "Jiho Han" <> wrote in message
    news:...
    > Kevin,
    >
    > I understand that the xml received by the soap endpoint is different.
    > However, when the xml document is deserialized into an object, there is no
    > difference - or rather I don't know of a way - between:
    >
    > <Person>
    > <Lastname xsi:nil = "true"/>
    > </Person>
    >
    > vs.
    >
    > <Person>
    > </Person>
    >
    > When it gets deserialized into an object,
    >
    > Person person = <from soap response>
    > person.Lastname == null // true for both!
    >
    > If there is a way to tell the difference, that'd be great. Is there some
    > kind of a hook into the soap deserialization scheme?
    >> An XML document, and in particular, a SOAP document, is not quite as
    >> simple as you seem to think. It can indicate whether the value is null
    >> or not. Example:
    >>
    >> <SomeObject xsi:nil="true" />
    >>
    >> Kevin Spencer
    >> Microsoft MVP
    >> Professional Chicken Salad Alchemist
    >> Sequence, Selection, Iteration.
    >>
    >> "Jiho Han" <> wrote in message
    >> news:...
    >>
    >>> Pablo,
    >>>
    >>> Perhaps, it is more of a design question...
    >>>
    >>> If you want a field to be set to null (think database), how would you
    >>> specify that from the client side?
    >>> If you don't include the field, it's null on the server. If you set
    >>> it to
    >>> null, it's still null on the server.
    >>> My point is that there seems to be no way of knowing whether the
    >>> client
    >>> intended a field to be set to null vs. skip the field for processing
    >>> becasue it's missing from the xml.
    >>> i.e.)
    >>> <Person>
    >>> <Lastname>Han</Lastname>
    >>> <Firstname/>
    >>> </Person>
    >>> and
    >>> <Person>
    >>> <Lastname>Han</Lastname>
    >>> </Person>
    >>> deserializes into an identical object state.
    >>>
    >>>> It is as simple as this,
    >>>>
    >>>> if( IsNullOrEmpty(person.lastname) )
    >>>> // Echo Is null
    >>>> else
    >>>> // Echo Is not null
    >>>> That is only valid for string types. For other kind of reference
    >>>> type,
    >>>> you should check if it is equal to null.
    >>>> if( person.lastname == null)
    >>>> // Echo Is null
    >>>> else
    >>>> // Echo Is not null
    >>>> "Jiho Han" <> wrote in message
    >>>> news:...
    >>>>> Here's the issue.
    >>>>>
    >>>>> You have a class,
    >>>>>
    >>>>> Class Person
    >>>>> {
    >>>>> public int id;
    >>>>> public string firstname;
    >>>>> public string lastname;
    >>>>> }
    >>>>> when you expose it via webservice,
    >>>>> [WebMethod]
    >>>>> public void UpdatePerson(Person)
    >>>>> {
    >>>>> ...
    >>>>> }
    >>>>> How do I know whether a field value has been specified or not? For
    >>>>> value
    >>>>> types, there is that matching XXXXSpecified buddy field, but for
    >>>>> string
    >>>>> type, there doesn't seem to be one.
    >>>>> So if incoming Xml looks something like this:
    >>>>> <Person>
    >>>>> <firstname>Jiho</firstname>
    >>>>> </Person>
    >>>>> when deserialized into a Person object, lastname field will contain
    >>>>> null, which doesn't tell me whether the field is simply missing
    >>>>> (thus
    >>>>> safely ignored) or lastname field should be set to null (in the
    >>>>> database, for example).
    >>>>> Any idea how to go about this?
    >>>>> Thanks in advance.
    >>>>> Jiho Han
    >>>>> Senior Software Engineer
    >>>>> Infinity Info Systems
    >>>>> The Sales Technology Experts
    >>>>> Tel: 212.563.4400 x216
    >>>>> Fax: 212.760.0540
    >>>>>
    >>>>> www.infinityinfo.com

    >
    >
    Kevin Spencer, Jul 25, 2006
    #6
  7. Jiho Han

    Jiho Han Guest

    Hello Kevin,

    I am not exactly sure that will help. It doesn't seem to matter how the
    SOAP is formatted, the end result is that when the XmlSerializer consumes
    an incoming xml, it's same. Once in object form, there is no way to tell
    which format it came in.
    This is a bit frustrating. Thanks for your help though.

    Jiho

    > I haven't personally encountered this situation. However, I think that
    > applying some custom formatting to the SOAP XSD might do the trick.
    > See http://msdn2.microsoft.com/en-us/library/k1y9z356.aspx
    >
    > Kevin Spencer
    > Microsoft MVP
    > Professional Chicken Salad Alchemist
    > Sequence, Selection, Iteration.
    >
    > "Jiho Han" <> wrote in message
    > news:...
    >
    >> Kevin,
    >>
    >> I understand that the xml received by the soap endpoint is different.
    >> However, when the xml document is deserialized into an object, there
    >> is no difference - or rather I don't know of a way - between:
    >>
    >> <Person>
    >> <Lastname xsi:nil = "true"/>
    >> </Person>
    >> vs.
    >>
    >> <Person>
    >> </Person>
    >> When it gets deserialized into an object,
    >>
    >> Person person = <from soap response>
    >> person.Lastname == null // true for both!
    >> If there is a way to tell the difference, that'd be great. Is there
    >> some kind of a hook into the soap deserialization scheme?
    >>
    >>> An XML document, and in particular, a SOAP document, is not quite as
    >>> simple as you seem to think. It can indicate whether the value is
    >>> null or not. Example:
    >>>
    >>> <SomeObject xsi:nil="true" />
    >>>
    >>> Kevin Spencer
    >>> Microsoft MVP
    >>> Professional Chicken Salad Alchemist
    >>> Sequence, Selection, Iteration.
    >>> "Jiho Han" <> wrote in message
    >>> news:...
    >>>> Pablo,
    >>>>
    >>>> Perhaps, it is more of a design question...
    >>>>
    >>>> If you want a field to be set to null (think database), how would
    >>>> you
    >>>> specify that from the client side?
    >>>> If you don't include the field, it's null on the server. If you
    >>>> set
    >>>> it to
    >>>> null, it's still null on the server.
    >>>> My point is that there seems to be no way of knowing whether the
    >>>> client
    >>>> intended a field to be set to null vs. skip the field for
    >>>> processing
    >>>> becasue it's missing from the xml.
    >>>> i.e.)
    >>>> <Person>
    >>>> <Lastname>Han</Lastname>
    >>>> <Firstname/>
    >>>> </Person>
    >>>> and
    >>>> <Person>
    >>>> <Lastname>Han</Lastname>
    >>>> </Person>
    >>>> deserializes into an identical object state.
    >>>>> It is as simple as this,
    >>>>>
    >>>>> if( IsNullOrEmpty(person.lastname) )
    >>>>> // Echo Is null
    >>>>> else
    >>>>> // Echo Is not null
    >>>>> That is only valid for string types. For other kind of reference
    >>>>> type,
    >>>>> you should check if it is equal to null.
    >>>>> if( person.lastname == null)
    >>>>> // Echo Is null
    >>>>> else
    >>>>> // Echo Is not null
    >>>>> "Jiho Han" <> wrote in message
    >>>>> news:...
    >>>>>> Here's the issue.
    >>>>>>
    >>>>>> You have a class,
    >>>>>>
    >>>>>> Class Person
    >>>>>> {
    >>>>>> public int id;
    >>>>>> public string firstname;
    >>>>>> public string lastname;
    >>>>>> }
    >>>>>> when you expose it via webservice,
    >>>>>> [WebMethod]
    >>>>>> public void UpdatePerson(Person)
    >>>>>> {
    >>>>>> ...
    >>>>>> }
    >>>>>> How do I know whether a field value has been specified or not?
    >>>>>> For
    >>>>>> value
    >>>>>> types, there is that matching XXXXSpecified buddy field, but for
    >>>>>> string
    >>>>>> type, there doesn't seem to be one.
    >>>>>> So if incoming Xml looks something like this:
    >>>>>> <Person>
    >>>>>> <firstname>Jiho</firstname>
    >>>>>> </Person>
    >>>>>> when deserialized into a Person object, lastname field will
    >>>>>> contain
    >>>>>> null, which doesn't tell me whether the field is simply missing
    >>>>>> (thus
    >>>>>> safely ignored) or lastname field should be set to null (in the
    >>>>>> database, for example).
    >>>>>> Any idea how to go about this?
    >>>>>> Thanks in advance.
    >>>>>> Jiho Han
    >>>>>> Senior Software Engineer
    >>>>>> Infinity Info Systems
    >>>>>> The Sales Technology Experts
    >>>>>> Tel: 212.563.4400 x216
    >>>>>> Fax: 212.760.0540
    >>>>>>
    >>>>>> www.infinityinfo.com
    Jiho Han, Jul 25, 2006
    #7
  8. Is there a difference between the two in the XML InfoSet model?

    John

    "Jiho Han" <> wrote in message
    news:...
    > Hello Kevin,
    >
    > I am not exactly sure that will help. It doesn't seem to matter how the
    > SOAP is formatted, the end result is that when the XmlSerializer consumes
    > an incoming xml, it's same. Once in object form, there is no way to tell
    > which format it came in.
    > This is a bit frustrating. Thanks for your help though.
    >
    > Jiho
    >
    >> I haven't personally encountered this situation. However, I think that
    >> applying some custom formatting to the SOAP XSD might do the trick.
    >> See http://msdn2.microsoft.com/en-us/library/k1y9z356.aspx
    >>
    >> Kevin Spencer
    >> Microsoft MVP
    >> Professional Chicken Salad Alchemist
    >> Sequence, Selection, Iteration.
    >>
    >> "Jiho Han" <> wrote in message
    >> news:...
    >>
    >>> Kevin,
    >>>
    >>> I understand that the xml received by the soap endpoint is different.
    >>> However, when the xml document is deserialized into an object, there
    >>> is no difference - or rather I don't know of a way - between:
    >>>
    >>> <Person>
    >>> <Lastname xsi:nil = "true"/>
    >>> </Person>
    >>> vs.
    >>>
    >>> <Person>
    >>> </Person>
    >>> When it gets deserialized into an object,
    >>>
    >>> Person person = <from soap response>
    >>> person.Lastname == null // true for both!
    >>> If there is a way to tell the difference, that'd be great. Is there
    >>> some kind of a hook into the soap deserialization scheme?
    >>>
    >>>> An XML document, and in particular, a SOAP document, is not quite as
    >>>> simple as you seem to think. It can indicate whether the value is
    >>>> null or not. Example:
    >>>>
    >>>> <SomeObject xsi:nil="true" />
    >>>>
    >>>> Kevin Spencer
    >>>> Microsoft MVP
    >>>> Professional Chicken Salad Alchemist
    >>>> Sequence, Selection, Iteration.
    >>>> "Jiho Han" <> wrote in message
    >>>> news:...
    >>>>> Pablo,
    >>>>>
    >>>>> Perhaps, it is more of a design question...
    >>>>>
    >>>>> If you want a field to be set to null (think database), how would
    >>>>> you
    >>>>> specify that from the client side?
    >>>>> If you don't include the field, it's null on the server. If you
    >>>>> set
    >>>>> it to
    >>>>> null, it's still null on the server.
    >>>>> My point is that there seems to be no way of knowing whether the
    >>>>> client
    >>>>> intended a field to be set to null vs. skip the field for
    >>>>> processing
    >>>>> becasue it's missing from the xml.
    >>>>> i.e.)
    >>>>> <Person>
    >>>>> <Lastname>Han</Lastname>
    >>>>> <Firstname/>
    >>>>> </Person>
    >>>>> and
    >>>>> <Person>
    >>>>> <Lastname>Han</Lastname>
    >>>>> </Person>
    >>>>> deserializes into an identical object state.
    >>>>>> It is as simple as this,
    >>>>>>
    >>>>>> if( IsNullOrEmpty(person.lastname) )
    >>>>>> // Echo Is null
    >>>>>> else
    >>>>>> // Echo Is not null
    >>>>>> That is only valid for string types. For other kind of reference
    >>>>>> type,
    >>>>>> you should check if it is equal to null.
    >>>>>> if( person.lastname == null)
    >>>>>> // Echo Is null
    >>>>>> else
    >>>>>> // Echo Is not null
    >>>>>> "Jiho Han" <> wrote in message
    >>>>>> news:...
    >>>>>>> Here's the issue.
    >>>>>>>
    >>>>>>> You have a class,
    >>>>>>>
    >>>>>>> Class Person
    >>>>>>> {
    >>>>>>> public int id;
    >>>>>>> public string firstname;
    >>>>>>> public string lastname;
    >>>>>>> }
    >>>>>>> when you expose it via webservice,
    >>>>>>> [WebMethod]
    >>>>>>> public void UpdatePerson(Person)
    >>>>>>> {
    >>>>>>> ...
    >>>>>>> }
    >>>>>>> How do I know whether a field value has been specified or not?
    >>>>>>> For
    >>>>>>> value
    >>>>>>> types, there is that matching XXXXSpecified buddy field, but for
    >>>>>>> string
    >>>>>>> type, there doesn't seem to be one.
    >>>>>>> So if incoming Xml looks something like this:
    >>>>>>> <Person>
    >>>>>>> <firstname>Jiho</firstname>
    >>>>>>> </Person>
    >>>>>>> when deserialized into a Person object, lastname field will
    >>>>>>> contain
    >>>>>>> null, which doesn't tell me whether the field is simply missing
    >>>>>>> (thus
    >>>>>>> safely ignored) or lastname field should be set to null (in the
    >>>>>>> database, for example).
    >>>>>>> Any idea how to go about this?
    >>>>>>> Thanks in advance.
    >>>>>>> Jiho Han
    >>>>>>> Senior Software Engineer
    >>>>>>> Infinity Info Systems
    >>>>>>> The Sales Technology Experts
    >>>>>>> Tel: 212.563.4400 x216
    >>>>>>> Fax: 212.760.0540
    >>>>>>>
    >>>>>>> www.infinityinfo.com

    >
    >
    John Saunders, Jul 26, 2006
    #8
  9. Jiho Han

    Jiho Han Guest

    John,

    I don't know, you tell me, are these two different?

    <Person>
    <FirstName xsi:nil = "true" />
    <Person>

    <Person>
    </Person>

    Thanks

    > Is there a difference between the two in the XML InfoSet model?
    >
    > John
    >
    > "Jiho Han" <> wrote in message
    > news:...
    >
    >> Hello Kevin,
    >>
    >> I am not exactly sure that will help. It doesn't seem to matter how
    >> the
    >> SOAP is formatted, the end result is that when the XmlSerializer
    >> consumes
    >> an incoming xml, it's same. Once in object form, there is no way to
    >> tell
    >> which format it came in.
    >> This is a bit frustrating. Thanks for your help though.
    >> Jiho
    >>
    >>> I haven't personally encountered this situation. However, I think
    >>> that applying some custom formatting to the SOAP XSD might do the
    >>> trick. See http://msdn2.microsoft.com/en-us/library/k1y9z356.aspx
    >>>
    >>> Kevin Spencer
    >>> Microsoft MVP
    >>> Professional Chicken Salad Alchemist
    >>> Sequence, Selection, Iteration.
    >>> "Jiho Han" <> wrote in message
    >>> news:...
    >>>> Kevin,
    >>>>
    >>>> I understand that the xml received by the soap endpoint is
    >>>> different. However, when the xml document is deserialized into an
    >>>> object, there is no difference - or rather I don't know of a way -
    >>>> between:
    >>>>
    >>>> <Person>
    >>>> <Lastname xsi:nil = "true"/>
    >>>> </Person>
    >>>> vs.
    >>>> <Person>
    >>>> </Person>
    >>>> When it gets deserialized into an object,
    >>>> Person person = <from soap response>
    >>>> person.Lastname == null // true for both!
    >>>> If there is a way to tell the difference, that'd be great. Is
    >>>> there
    >>>> some kind of a hook into the soap deserialization scheme?
    >>>>> An XML document, and in particular, a SOAP document, is not quite
    >>>>> as simple as you seem to think. It can indicate whether the value
    >>>>> is null or not. Example:
    >>>>>
    >>>>> <SomeObject xsi:nil="true" />
    >>>>>
    >>>>> Kevin Spencer
    >>>>> Microsoft MVP
    >>>>> Professional Chicken Salad Alchemist
    >>>>> Sequence, Selection, Iteration.
    >>>>> "Jiho Han" <> wrote in message
    >>>>> news:...
    >>>>>> Pablo,
    >>>>>>
    >>>>>> Perhaps, it is more of a design question...
    >>>>>>
    >>>>>> If you want a field to be set to null (think database), how would
    >>>>>> you
    >>>>>> specify that from the client side?
    >>>>>> If you don't include the field, it's null on the server. If you
    >>>>>> set
    >>>>>> it to
    >>>>>> null, it's still null on the server.
    >>>>>> My point is that there seems to be no way of knowing whether the
    >>>>>> client
    >>>>>> intended a field to be set to null vs. skip the field for
    >>>>>> processing
    >>>>>> becasue it's missing from the xml.
    >>>>>> i.e.)
    >>>>>> <Person>
    >>>>>> <Lastname>Han</Lastname>
    >>>>>> <Firstname/>
    >>>>>> </Person>
    >>>>>> and
    >>>>>> <Person>
    >>>>>> <Lastname>Han</Lastname>
    >>>>>> </Person>
    >>>>>> deserializes into an identical object state.
    >>>>>>> It is as simple as this,
    >>>>>>>
    >>>>>>> if( IsNullOrEmpty(person.lastname) )
    >>>>>>> // Echo Is null
    >>>>>>> else
    >>>>>>> // Echo Is not null
    >>>>>>> That is only valid for string types. For other kind of reference
    >>>>>>> type,
    >>>>>>> you should check if it is equal to null.
    >>>>>>> if( person.lastname == null)
    >>>>>>> // Echo Is null
    >>>>>>> else
    >>>>>>> // Echo Is not null
    >>>>>>> "Jiho Han" <> wrote in message
    >>>>>>> news:...
    >>>>>>>> Here's the issue.
    >>>>>>>>
    >>>>>>>> You have a class,
    >>>>>>>>
    >>>>>>>> Class Person
    >>>>>>>> {
    >>>>>>>> public int id;
    >>>>>>>> public string firstname;
    >>>>>>>> public string lastname;
    >>>>>>>> }
    >>>>>>>> when you expose it via webservice,
    >>>>>>>> [WebMethod]
    >>>>>>>> public void UpdatePerson(Person)
    >>>>>>>> {
    >>>>>>>> ...
    >>>>>>>> }
    >>>>>>>> How do I know whether a field value has been specified or not?
    >>>>>>>> For
    >>>>>>>> value
    >>>>>>>> types, there is that matching XXXXSpecified buddy field, but
    >>>>>>>> for
    >>>>>>>> string
    >>>>>>>> type, there doesn't seem to be one.
    >>>>>>>> So if incoming Xml looks something like this:
    >>>>>>>> <Person>
    >>>>>>>> <firstname>Jiho</firstname>
    >>>>>>>> </Person>
    >>>>>>>> when deserialized into a Person object, lastname field will
    >>>>>>>> contain
    >>>>>>>> null, which doesn't tell me whether the field is simply missing
    >>>>>>>> (thus
    >>>>>>>> safely ignored) or lastname field should be set to null (in the
    >>>>>>>> database, for example).
    >>>>>>>> Any idea how to go about this?
    >>>>>>>> Thanks in advance.
    >>>>>>>> Jiho Han
    >>>>>>>> Senior Software Engineer
    >>>>>>>> Infinity Info Systems
    >>>>>>>> The Sales Technology Experts
    >>>>>>>> Tel: 212.563.4400 x216
    >>>>>>>> Fax: 212.760.0540
    >>>>>>>>
    >>>>>>>> www.infinityinfo.com
    Jiho Han, Jul 27, 2006
    #9
  10. Jiho Han

    Jiho Han Guest

    (Answering my own question) from the w3c rec,

    2.6.2 xsi:nil

    XML Schema: Structures introduces a mechanism for signaling that an element
    should be accepted as ·valid· when it has no content despite a content type
    which does not require or even necessarily allow empty content. An element
    may be ·valid· without content if it has the attribute xsi:nil with the value
    true. An element so labeled must be empty, but can carry attributes if permitted
    by the corresponding complex type.

    which tells me, "nillability" is strictly about the content of an element.
    So in that case, a missing optional element vs. a nillable element which
    possibly can carry attributes, are different.

    btw, in my schema, FirstName would be defined this ways:

    <xs:element name="FirstName" minOccurs="0" nillable="true"/>

    Thanks
    Jiho

    > John,
    >
    > I don't know, you tell me, are these two different?
    >
    > <Person>
    > <FirstName xsi:nil = "true" />
    > <Person>
    > <Person>
    > </Person>
    > Thanks
    >
    >> Is there a difference between the two in the XML InfoSet model?
    >>
    >> John
    >>
    >> "Jiho Han" <> wrote in message
    >> news:...
    >>> Hello Kevin,
    >>>
    >>> I am not exactly sure that will help. It doesn't seem to matter how
    >>> the
    >>> SOAP is formatted, the end result is that when the XmlSerializer
    >>> consumes
    >>> an incoming xml, it's same. Once in object form, there is no way to
    >>> tell
    >>> which format it came in.
    >>> This is a bit frustrating. Thanks for your help though.
    >>> Jiho
    >>>> I haven't personally encountered this situation. However, I think
    >>>> that applying some custom formatting to the SOAP XSD might do the
    >>>> trick. See http://msdn2.microsoft.com/en-us/library/k1y9z356.aspx
    >>>>
    >>>> Kevin Spencer
    >>>> Microsoft MVP
    >>>> Professional Chicken Salad Alchemist
    >>>> Sequence, Selection, Iteration.
    >>>> "Jiho Han" <> wrote in message
    >>>> news:...
    >>>>> Kevin,
    >>>>>
    >>>>> I understand that the xml received by the soap endpoint is
    >>>>> different. However, when the xml document is deserialized into an
    >>>>> object, there is no difference - or rather I don't know of a way -
    >>>>> between:
    >>>>>
    >>>>> <Person>
    >>>>> <Lastname xsi:nil = "true"/>
    >>>>> </Person>
    >>>>> vs.
    >>>>> <Person>
    >>>>> </Person>
    >>>>> When it gets deserialized into an object,
    >>>>> Person person = <from soap response>
    >>>>> person.Lastname == null // true for both!
    >>>>> If there is a way to tell the difference, that'd be great. Is
    >>>>> there
    >>>>> some kind of a hook into the soap deserialization scheme?
    >>>>>> An XML document, and in particular, a SOAP document, is not quite
    >>>>>> as simple as you seem to think. It can indicate whether the value
    >>>>>> is null or not. Example:
    >>>>>>
    >>>>>> <SomeObject xsi:nil="true" />
    >>>>>>
    >>>>>> Kevin Spencer
    >>>>>> Microsoft MVP
    >>>>>> Professional Chicken Salad Alchemist
    >>>>>> Sequence, Selection, Iteration.
    >>>>>> "Jiho Han" <> wrote in message
    >>>>>> news:...
    >>>>>>> Pablo,
    >>>>>>>
    >>>>>>> Perhaps, it is more of a design question...
    >>>>>>>
    >>>>>>> If you want a field to be set to null (think database), how
    >>>>>>> would
    >>>>>>> you
    >>>>>>> specify that from the client side?
    >>>>>>> If you don't include the field, it's null on the server. If you
    >>>>>>> set
    >>>>>>> it to
    >>>>>>> null, it's still null on the server.
    >>>>>>> My point is that there seems to be no way of knowing whether the
    >>>>>>> client
    >>>>>>> intended a field to be set to null vs. skip the field for
    >>>>>>> processing
    >>>>>>> becasue it's missing from the xml.
    >>>>>>> i.e.)
    >>>>>>> <Person>
    >>>>>>> <Lastname>Han</Lastname>
    >>>>>>> <Firstname/>
    >>>>>>> </Person>
    >>>>>>> and
    >>>>>>> <Person>
    >>>>>>> <Lastname>Han</Lastname>
    >>>>>>> </Person>
    >>>>>>> deserializes into an identical object state.
    >>>>>>>> It is as simple as this,
    >>>>>>>>
    >>>>>>>> if( IsNullOrEmpty(person.lastname) )
    >>>>>>>> // Echo Is null
    >>>>>>>> else
    >>>>>>>> // Echo Is not null
    >>>>>>>> That is only valid for string types. For other kind of
    >>>>>>>> reference
    >>>>>>>> type,
    >>>>>>>> you should check if it is equal to null.
    >>>>>>>> if( person.lastname == null)
    >>>>>>>> // Echo Is null
    >>>>>>>> else
    >>>>>>>> // Echo Is not null
    >>>>>>>> "Jiho Han" <> wrote in message
    >>>>>>>> news:...
    >>>>>>>>> Here's the issue.
    >>>>>>>>>
    >>>>>>>>> You have a class,
    >>>>>>>>>
    >>>>>>>>> Class Person
    >>>>>>>>> {
    >>>>>>>>> public int id;
    >>>>>>>>> public string firstname;
    >>>>>>>>> public string lastname;
    >>>>>>>>> }
    >>>>>>>>> when you expose it via webservice,
    >>>>>>>>> [WebMethod]
    >>>>>>>>> public void UpdatePerson(Person)
    >>>>>>>>> {
    >>>>>>>>> ...
    >>>>>>>>> }
    >>>>>>>>> How do I know whether a field value has been specified or not?
    >>>>>>>>> For
    >>>>>>>>> value
    >>>>>>>>> types, there is that matching XXXXSpecified buddy field, but
    >>>>>>>>> for
    >>>>>>>>> string
    >>>>>>>>> type, there doesn't seem to be one.
    >>>>>>>>> So if incoming Xml looks something like this:
    >>>>>>>>> <Person>
    >>>>>>>>> <firstname>Jiho</firstname>
    >>>>>>>>> </Person>
    >>>>>>>>> when deserialized into a Person object, lastname field will
    >>>>>>>>> contain
    >>>>>>>>> null, which doesn't tell me whether the field is simply
    >>>>>>>>> missing
    >>>>>>>>> (thus
    >>>>>>>>> safely ignored) or lastname field should be set to null (in
    >>>>>>>>> the
    >>>>>>>>> database, for example).
    >>>>>>>>> Any idea how to go about this?
    >>>>>>>>> Thanks in advance.
    >>>>>>>>> Jiho Han
    >>>>>>>>> Senior Software Engineer
    >>>>>>>>> Infinity Info Systems
    >>>>>>>>> The Sales Technology Experts
    >>>>>>>>> Tel: 212.563.4400 x216
    >>>>>>>>> Fax: 212.760.0540
    >>>>>>>>>
    >>>>>>>>> www.infinityinfo.com
    Jiho Han, Jul 27, 2006
    #10
  11. Jiho Han" <> wrote in message
    news:...
    > John,
    >
    > I don't know, you tell me, are these two different?
    >
    > <Person>
    > <FirstName xsi:nil = "true" />
    > <Person>
    >
    > <Person>
    > </Person>
    >


    I don't know, either, which is why I asked the question.

    They certainly don't appear to be different in the intent of the document
    creator.

    John
    John Saunders, Jul 27, 2006
    #11
  12. "Jiho Han" <> wrote in message
    news:...
    > (Answering my own question) from the w3c rec,
    >
    > 2.6.2 xsi:nil
    >
    > XML Schema: Structures introduces a mechanism for signaling that an
    > element should be accepted as ·valid· when it has no content despite a
    > content type which does not require or even necessarily allow empty
    > content. An element may be ·valid· without content if it has the attribute
    > xsi:nil with the value true. An element so labeled must be empty, but can
    > carry attributes if permitted by the corresponding complex type.
    >
    > which tells me, "nillability" is strictly about the content of an element.
    > So in that case, a missing optional element vs. a nillable element which
    > possibly can carry attributes, are different.
    >
    > btw, in my schema, FirstName would be defined this ways:
    >
    > <xs:element name="FirstName" minOccurs="0" nillable="true"/>


    Ok, so is there a difference to you between a person with no first name, and
    a person with ... no first name?

    John
    John Saunders, Jul 27, 2006
    #12
  13. Jiho Han

    Jiho Han Guest

    Actually, the intention is what you make of it in this case, because I am
    the publisher of the schema.

    If I am accepting the document for an update method, for example, I could
    take the first form as saying set FirstName to null and the second form as
    saying, well, nothing. Don't do anything to FirstName at all.

    All this to say, the default xml deserialization does not distinguish between
    the first and the latter.
    I think, though, I can use a custom schema provider for this, then implement
    IXmlSerializable on the class so that I can examine the xsi:nil attribute
    upon deserializing. We'll see how that goes.

    > Jiho Han" <> wrote in message
    > news:...
    >
    >> John,
    >>
    >> I don't know, you tell me, are these two different?
    >>
    >> <Person>
    >> <FirstName xsi:nil = "true" />
    >> <Person>
    >> <Person>
    >> </Person>

    > I don't know, either, which is why I asked the question.
    >
    > They certainly don't appear to be different in the intent of the
    > document creator.
    >
    > John
    >
    Jiho Han, Jul 27, 2006
    #13
  14. You could also try changing the schema to include the two alternatives
    clearly specified. You could, for instance, create a global "doNotTouch"
    attribute, and use it on the <FirstName> or wherever else. The sender could
    explicitly indicate what he wants to do, and this question of similarities
    would be moot.

    John


    "Jiho Han" <> wrote in message
    news:...
    > Actually, the intention is what you make of it in this case, because I am
    > the publisher of the schema.
    >
    > If I am accepting the document for an update method, for example, I could
    > take the first form as saying set FirstName to null and the second form as
    > saying, well, nothing. Don't do anything to FirstName at all.
    >
    > All this to say, the default xml deserialization does not distinguish
    > between the first and the latter.
    > I think, though, I can use a custom schema provider for this, then
    > implement IXmlSerializable on the class so that I can examine the xsi:nil
    > attribute upon deserializing. We'll see how that goes.
    >
    >> Jiho Han" <> wrote in message
    >> news:...
    >>
    >>> John,
    >>>
    >>> I don't know, you tell me, are these two different?
    >>>
    >>> <Person>
    >>> <FirstName xsi:nil = "true" />
    >>> <Person>
    >>> <Person>
    >>> </Person>

    >> I don't know, either, which is why I asked the question.
    >>
    >> They certainly don't appear to be different in the intent of the
    >> document creator.
    >>
    >> John
    >>

    >
    >
    John Saunders, Jul 29, 2006
    #14
    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. Miguel Dias Moura
    Replies:
    2
    Views:
    340
    Martin
    Jun 22, 2004
  2. Hazz
    Replies:
    1
    Views:
    390
    =?Utf-8?B?SGFtbWFkIFJhampvdWI=?=
    Mar 17, 2005
  3. Jeannie
    Replies:
    15
    Views:
    887
    Jeannie
    Aug 30, 2005
  4. yogesh
    Replies:
    1
    Views:
    366
    Victor Bazarov
    Mar 14, 2007
  5. Mike A
    Replies:
    17
    Views:
    261
    Dr John Stockton
    Nov 19, 2003
Loading...

Share This Page