Nullable value with xsd.exe generated class

前端 未结 3 1497
刺人心
刺人心 2021-02-19 04:45

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:



        
相关标签:
3条回答
  • 2021-02-19 05:14

    I believe if you add nillable="true" in your XML schema definition, XSD will create a nullable type for that element. This is probably a good idea anyway, since you are implying that this value is indeed nillable and this would make your schema definition more explicit in that regard.

    Unfortunately, it still includes the corresponding "Specified" field in this case. If you want the serializer to obey the nullable value directly, you will need to manually remove the "xxSpecified" column from the generated code.

    0 讨论(0)
  • 2021-02-19 05:17

    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;
        }
    }
    
    0 讨论(0)
  • 2021-02-19 05:19

    I've just noticed that it has actually included the following code:

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

    Which provides the functionality I need.

    I'll leave the question open for a while in case there is an elegant way to make use of nullable? type instead.

    0 讨论(0)
提交回复
热议问题