Optional fields are not serialized properly

Dec 19, 2008 at 10:50 AM
Aloha,

I have an xsd with the fragment:

 <xs:element name="Gender" type="ksgender" minOccurs="0" maxOccurs="1">
        <xs:annotation>
          <xs:documentation>In: "male" or "female".</xs:documentation>
        </xs:annotation>
      </xs:element>

This generated the following code:

[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public ksgender Gender
{
    get
    {
        return this.GenderField;
    }
    set
    {
        this.GenderField = value;
    }
}

[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool GenderSpecified
{
    get
    {
        return this.GenderFieldSpecified;
    }
    set
    {
        this.GenderFieldSpecified = value;
    }
}

When setting the gender in the generated class (class.Gender = ksgender.male) and the XML serializing the class, the Gender field is not serialized, because GenderFieldSpecified
remains false! The generated should generate the setter property like this:
    set
    {
       this.GenderField = value;
       this.GenderFieldSpecified = true;
    }