当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Use xsd to create a Nullable<int> object in generated class

问题描述:

I am using the xsd to create a .net class, but I can't seem to get an int? as one of the properties on the generated class. Is there any way to do this?

网友答案:

Set attribute nillable="true" for xs:element

xsd.exe generated:

<?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>

for this class:

public class Class1
{
    public int? MyInt { get; set; }
}

Reverse conversion gives the same class:

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

I used C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\xsd.exe, .NET Framework, Version 4.0.30319.33440

分享给朋友:
您可能感兴趣的文章:
随机阅读: