Search code examples
c#web-servicessoapwsdl

Setting minOccurs=0 in a WSDL using "Specified" pattern not working


OK, obviously I'm doing something wrong here. I'm trying to create a webservice and I want "dateShipped" to be optional which means in the WSDL, I want minOccurs="0"

[Serializable]
[XmlType]
public class CTShipment
{
    [XmlElement(Order = 0, IsNullable=false)] public CTDeliveryMethod DeliveryMethod;
    [XmlElement(Order = 1, IsNullable=false)] public CTShipmentAddress ShipmentAddress;
    [XmlIgnore] public bool dateShippedSpecified;
    [XmlElement(Order = 2, IsNullable=false)] public DateTime dateShipped;
}

I want the WSDL to be generated like this:

<xs:complexType name="CTShipment">
  <xs:annotation>
     <xs:documentation>All details for the shipment of a suborder.</xs:documentation>
  </xs:annotation>
  <xs:sequence>
     <xs:element name="DeliveryMethod" type="CTDeliveryMethod" nillable="false"/>
     <xs:element name="ShipmentAddress" type="CTShipmentAddress" nillable="false"/>
     <xs:element name="dateShipped" type="xs:dateTime" nillable="false" minOccurs="0"/>
  </xs:sequence>
</xs:complexType>

Instead what I am actually getting is this:

<xs:complexType name="CTShipment">
  <xs:sequence>
     <xs:element name="DeliveryMethod" nillable="true" type="tns:CTDeliveryMethod"/>
     <xs:element name="ShipmentAddress" nillable="true" type="tns:CTShipmentAddress"/>
     <xs:element name="dateShipped" type="xs:dateTime"/>
     <xs:element name="dateShippedSpecified" type="xs:boolean"/>
  </xs:sequence>
</xs:complexType>

According to several things I've read (including http://msdn.microsoft.com/en-us/library/zds0b35c%28v=vs.90%29.aspx) including the public bool "dateShippedSpecified" should make "dateShipped" optional (minOccurs=0). As you can see, not only is this not happening but "dateShippedSpecified" is showing up in the WSDL even though it is marked with "[XmlIgnore]". You may have noticed that there is another problem as well: even though I'm specifying "IsNullable=false", I still get nillable="true" in the WSDL.

That is no less than 4 problems I can't explain all related to the same thing:

  1. How can I set minOccurs to 0 in my WSDL?

  2. Why isn't the [fieldName]Specified pattern making [fieldName] optional (minOccurs = 0)?

  3. Even if it wasn't following the ___Specified pattern, why would dateShippedSpecified show up in the WSDL if it is marked with XmlIgnore?

  4. Why is everything marked as nillable="true" even though I'm specifying "IsNullable=false"?

    and as a bonus question, if anyone knows...

  5. How do I get the annotation (as shown below) to be included?

    <xs:annotation>
     <xs:documentation>All details for the shipment of a suborder.</xs:documentation>
    </xs:annotation>
    

Solution

  • This is due to Sequence element. It specifies that every element has minOccurs= 1. And the WSDL uses a Sequence-Element instead of "All" because you specified an Order to them. This requires that every value is present.

    So it should work allready when you remove the Order. If you really need the order, than there is no way for you to leave out that value.