Nullable value with xsd.exe generated class

Don Vince picture Don Vince · Sep 14, 2009 · Viewed 10.9k times · Source

I have been using xsd.exe to generate a class for deserializing XML into. I have decimal value in the source xsd that is not required:

<xs:attribute name="Balance" type="xs:decimal" use="optional" />

The resulting class from xsd generates the following code:

private decimal balanceField;

[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal Balance {
    get {
        return this.balanceField;
    }
    set {
        this.balanceField = value;
    }
}

Which I note is not nullable.

How do I instead generate the field as nullable, illustrated as follows:

private decimal? balanceField;

[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal? Balance {
    get {
        return this.balanceField;
    }
    set {
        this.balanceField = value;
    }
}

Answer

Michał Powaga picture Michał Powaga · Mar 8, 2013

Currently it works as it should. I'm using xsd v2.0.50727.42 and:

<xs:element name="Port" type="xs:int" nillable="true" />

generates exactly what you've been looking for (without redundant ...Specified field and property):

private System.Nullable<int> portField;

[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public System.Nullable<int> Port {
    get {
        return this.portField;
    }
    set {
        this.portField = value;
    }
}