与XSD.EXE生成的类空值 [英] Nullable value with xsd.exe generated class

查看:168
本文介绍了与XSD.EXE生成的类空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用XSD.EXE生成XML反序列化到一个类。
我有一个不需要源XSD十进制值:

 < XS:属性名称=平衡TYPE =XS:十进制使用=可选/> 



从XSD生成的类生成下面的代码:

 私人小数balanceField; 

[System.Xml.Serialization.XmlAttributeAttribute()]
公共小数平衡{
获得{
返回this.balanceField;
}
集合{
this.balanceField =价值;
}
}



这我注意的是不能为空。



我如何,而不是生成的字段为空,说明如下:

 私人十进制? balanceField; 

[System.Xml.Serialization.XmlAttributeAttribute()]
公共小数?平衡{
获得{
返回this.balanceField;
}
集合{
this.balanceField =价值;
}
}


解决方案

目前它的工作原理,因为它应该。我使用的XSD v2.0.50727.42和:



<预类=郎咸平的XML prettyprint-覆盖> < XS:元素的名称= 端口类型=XS:INT的nillable =真/>



产生正是你一直在寻找的东西(没有多余的 ...指定字段和属性):



<预类=郎-CS prettyprint-覆盖> 私人System.Nullable< INT> ;宝辉;

[System.Xml.Serialization.XmlElementAttribute(ISNULLABLE =真)]
公共System.Nullable< INT>端口{
获得{
返回this.portField;
}
集合{
this.portField =价值;
}
}


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;
    }
}

解决方案

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;
    }
}

这篇关于与XSD.EXE生成的类空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆