views:

1560

answers:

3

I have a .NET Web app which consumes a Java-based Web service. One of the objects, named Optional, contains search criteria fields. The schema is the following:

<xsd:complexType name="Optional">
 <xsd:sequence>
  <xsd:element name="FromAmount" nillable="true" type="xsd:float" minOccurs="0" /> 
  <xsd:element name="ToAmount" nillable="true" type="xsd:float" minOccurs="0" /> 
  <xsd:element name="FromDate" nillable="true" type="xsd:dateTime" minOccurs="0" /> 
  <xsd:element name="ToDate" nillable="true" type="xsd:dateTime" minOccurs="0" /> 
  <xsd:element name="FromCheckNumber" nillable="true" type="xsd:long" minOccurs="0" /> 
  <xsd:element name="ToCheckNumber" nillable="true" type="xsd:long" minOccurs="0" /> 
 </xsd:sequence>
</xsd:complexType>

The problem that I am running into is that the child elements will not serialize even when a value is assigned to them in the Web app. If I remove the minOccurs attribute, then all is well.

How do I get these elements to be optional, but to serialize when a value is assigned to them?

Thanks in advance for your help.

A: 

This schema does not define any xml document type. It just provides a declaration for a compex type named "Optional", but there is no reference to this type from anywhere.

The xml document being defined must have at least a top element. this top element needs to be defined somewhere (at a global scope). There is no such definition in the provided schema.

A minimal example of an xml schema, which is similar to the provided one, but does define an xml document is the following:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema 
    elementFormDefault="qualified"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>
  <xsd:element name="Optional" type="Optional"/>
  <xsd:complexType name="Optional">
    <xsd:sequence>
      <xsd:element name="FromAmount" nillable="true" type="xsd:float" minOccurs="0" />
      <xsd:element name="ToAmount" nillable="true" type="xsd:float" minOccurs="0" />
      <xsd:element name="FromDate" nillable="true" type="xsd:dateTime" minOccurs="0" />
      <xsd:element name="ToDate" nillable="true" type="xsd:dateTime" minOccurs="0" />
      <xsd:element name="FromCheckNumber" nillable="true" type="xsd:long" minOccurs="0" />
      <xsd:element name="ToCheckNumber" nillable="true" type="xsd:long" minOccurs="0" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>

and the simplest xml document that can be successfully validated against this schema is just

    <Optional/>

(since all of the children of the top element are defined as optional).

Hope this helped.

Cheers,

Dimitre Novatchev

Dimitre Novatchev
Dimitre,Thank you for your response. I apologize for not clarifying that the xml which is posted is just an excerpt from the WSDL. The document is well-formed, but I did not post the entire WSDL because I did not think it was relevant to the problem I'm experiencing.
Mondo
Mondo, thanks for clarifying this. How about defining the problem even better? A correct definition of the problem is a necessary prerequisete to anyone understanding it and being able to help.
Dimitre Novatchev
+1  A: 

in .NET WS for non-nullable types (in .NET) that are marked optional in the schema have an additional specified property generated for them that control if the element appears. Very annoyingly, the setter for the value does not set the additional specified flag, so you need do this.

x.ToAmmount = 24.0f;
x.ToAmmountSpecified = true;
// etc for the rest of the poperties
superfell
A: 

My understanding is that nillable="true" generates a xsi:nil="true" in the value XML if the value is not present, meaning that the element is always created, even if the value is null.

Try removing the nillable attribute and keeping the minOccurs="0". Haven't tried it though.

devio