none
minOccurs="0" and nillable="true"

    Question

  • I tried to create a message contract to achieve the following characteristic but till now still NO luck to get the result successfully.  We have tried multiple ways suggested by different articles, including using XxxSpecified concept. We understand that by using IsNullable=True property, it will automatically emit minOccurs="1" and nillable="true", but we want to change the behavior so that the minOccurs="0".

    <xsd:element name="LocationCode" type="xsdTongue Tiedtring" minOccurs="0" nillable="true"/>


    The data contract looks like the followoing.


     [System.ServiceModel.MessageContractAttribute(
        WrapperName = "AddUpdateNormalScheduleRequest",
         WrapperNamespace = "Schooldude:ConnectSchedules", IsWrapped = true),]
        [System.SerializableAttribute()]


        public class AddUpdateNormalScheduleReq
        {

               :
    :

            [System.ServiceModel.MessageBodyMemberAttribute(Order = 6)]
            [System.Xml.Serialization.XmlElement(IsNullable = true)]
            public string LocationCode;

            [System.Xml.Serialization.XmlIgnore()]
            public bool LocationCodeSpecified;


    Please advice and thanks.

     

    Wednesday, July 02, 2008 9:46 AM

Answers

  • Hi Pang,

     

         From the Schema prespective the WSDL is valid to have the minOccurs="0" and nillable="true", and so it's right, but in this situation it doesn't seems right to have both because when serializing you will need to have something saying what happened with your value, and that's why when you place the Nullable=True on the XElementAttribute it will generate the minOccurs="1".

     

        About the second part, it's wiered what happens with the DefaultValue, but that I can't explain. sorry.

     

    Hope this helps you.

     

    Wednesday, July 02, 2008 11:34 AM

All replies

  •  

    Hi Pang,

     

          When you use the System.Xml.Serialization.XmlElement(IsNullable = true), then there's no way you can have the minOccurs="0", because what the IsNullable does it when the value that will be serialized is Null than it will be generated like the following:

              <LocationCode xsi:nil="true"/>, and so this will always be part of the serialization result, and that's why the minOccurs always appears with 1.

     

    Hope this helps.

    Wednesday, July 02, 2008 11:00 AM
  • Hi Nuno Godinho, thanks for the info. Let us forget the issue from the specified technical perspective for a while, is it (from the open standard perspective) valid for a schema in the WSDL to have minOccurs="0" and nillable="true"? Can i say that it seems that it is "Not Valid"?

    And for your information, the "weird" thing is: let say we add the following attribute to the property (with String dataType) in message contract class:
    [System.ComponentModel.DefaultValue("")]

    We get minOccurs="0", nillable="true", but with another attribute default="".

    Please advice
    Wednesday, July 02, 2008 11:07 AM
  • Hi Pang,

     

         From the Schema prespective the WSDL is valid to have the minOccurs="0" and nillable="true", and so it's right, but in this situation it doesn't seems right to have both because when serializing you will need to have something saying what happened with your value, and that's why when you place the Nullable=True on the XElementAttribute it will generate the minOccurs="1".

     

        About the second part, it's wiered what happens with the DefaultValue, but that I can't explain. sorry.

     

    Hope this helps you.

     

    Wednesday, July 02, 2008 11:34 AM
  • I am writing to note an ancilliary behavior of having both minoccurs="0" and nillable="true" in an xsd element spec used by a Wcf Soap service configured to use DataContractSerializer. 

    For example, let's say your xsd contains this fragment:

    <xs:element minOccurs="0" name="Gender" nillable="true" type="...

    What I observe is that if the incoming client xml data omits <Gender/>, then when the Wcf Service serializes the incoming data, it will insert the element Gender as:

     <Gender xsi:nil="true"/>
    
    

    In our case, the author wanted <Gender> to not appear if the element had no value, but that was not possible with the xsd declared as it was.  Hope this helps any readers out there.  Thank you.  Live long and prosper.


    • Edited by SpockMonster Friday, September 02, 2011 3:37 PM spelling correction
    Friday, September 02, 2011 3:36 PM