我一直在使用 xsd.exe 来生成一个用于反序列化 XML 的类。 我在不需要的源 xsd 中有十进制值:
<xs:attribute name="Balance" type="xs:decimal" use="optional" />
xsd 生成的类生成以下代码:
private decimal balanceField;
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal Balance {
get {
return this.balanceField;
}
set {
this.balanceField = value;
}
}
我注意到这是不可空的。
我如何改为将字段生成为可为空,如下所示:
private decimal? balanceField;
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal? Balance {
get {
return this.balanceField;
}
set {
this.balanceField = value;
}
}
最佳答案
目前它可以正常工作。我正在使用 xsd v2.0.50727.42 并且:
<xs:element name="Port" type="xs:int" nillable="true" />
准确生成您一直在寻找的内容(没有多余的 ...Specified
字段和属性):
private System.Nullable<int> portField;
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public System.Nullable<int> Port {
get {
return this.portField;
}
set {
this.portField = value;
}
}
关于c# - xsd.exe 生成类的可空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1420930/