4

I have an xsd file that has the following definition below. When using xsd.exe to generate classes from the xsd file, the enum attrs get an additional FieldSpecified property as visible below. Unless the FieldSpecified property is set, the value will not serialize with the value of the attribute. Is there an additional property I can add to the xsd or a flag I can use with xsd.exe to always cause the value to be serialized?

Example from xsd:

<xs:simpleType name="adrLn">
  <xs:restriction base="xs:string">
    <xs:enumeration value="ST" />
    <xs:enumeration value="APTN" />
  </xs:restriction>
</xs:simpleType>

...

<xs:element name="AddressLine" minOccurs="0" maxOccurs="unbounded">
  <xs:complexType>
    <xs:attribute name="AddrLineTypCd" type="adrLn" />
  </xs:complexType>
</xs:element>

Example from generated code:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class RequestCheckIssueAddressAddressLine {

    private adrLn addrLineTypCdField;

    private bool addrLineTypCdFieldSpecified;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public adrLn AddrLineTypCd {
        get {
            return this.addrLineTypCdField;
        }
        set {
            this.addrLineTypCdField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool AddrLineTypCdSpecified {
        get {
            return this.addrLineTypCdFieldSpecified;
        }
        set {
            this.addrLineTypCdFieldSpecified = value;
        }
    }
}
QueueHammer
  • 10,515
  • 12
  • 67
  • 91

1 Answers1

1

There is no flag to change the behaviour - it is all driven by the XSD.

Enums are non nullable. Your attribute is optional (the default value of the use attribute in XSD), and so the xxxSpecified properties are needed to control the serialization of the associated fields (in your case the addrLineTypCdField field).

Since you've indicated changing the XSD as a possibility, then the following should fix your issue (make the attribute required):

<xs:attribute name="AddrLineTypCd" type="adrLn" use="required" />
Petru Gardea
  • 21,373
  • 2
  • 50
  • 62
  • I wish it would implement the field with a nullable backing, or flag the field as for serialization if the setter has been used. After posting this I noticed it does this for numeric values too. – QueueHammer Feb 03 '17 at 16:42