XML serialization issue for minoccurs

I have a .NET web application that uses a Java based web service. One of the objects named Advanced contains search criteria fields. The scheme is as follows:

<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 I am running into is that the child elements will not be serialized even when they are assigned a value in the web application. If I remove the minOccurs attribute then everything is fine.

How do I get these elements as optional, but serialize when they are assigned a value?

Thanks in advance for your help.

+1


source to share


3 answers


in .NET WS for non-empty types (in .NET) that are marked as optional in the schema have an additional set property generated for them that control if an item appears. Very annoyingly, the setter for the value doesn't set the extra specified flag, so you have to do that.



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

      

+3


source


This schema does not define the xml document type. It just provides a declaration for a type compex named Optional, but this type is not referenced anywhere.

The xml document being defined must have at least a top element. this top element must be defined somewhere (globally). There is no such definition in the above diagram.

A minimal example of an xml schema that is similar to the one provided but defines an XML document looks like this:

<? 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 simply



    <Optional/>

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

Hope it helped.

Greetings,

Dimitar Novachev

0


source


I understand that nillable = "true" generates xsi: nil = "true" in the XML value if there is no value, which means that the element is always created even if the value is null.

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

0


source







All Articles