我正在使用xsd创建一个.net类,但我似乎无法获得一个int?作为生成的类的属性之一。有没有办法做到这一点?
答案 0 :(得分:2)
为nillable="true"
xs:element
xsd.exe生成:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Class1" nillable="true" type="Class1" />
<xs:complexType name="Class1">
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="1" name="MyInt" nillable="true" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:schema>
这个班级:
public class Class1
{
public int? MyInt { get; set; }
}
反转换提供相同的类:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=true)]
public partial class Class1 {
private System.Nullable<int> myIntField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
public System.Nullable<int> MyInt {
get {
return this.myIntField;
}
set {
this.myIntField = value;
}
}
}
我使用了C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\xsd.exe
,.NET Framework,版本4.0.30319.33440