1

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?

Anders Miltner
  • 253
  • 3
  • 12

1 Answers1

3

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

dizel3d
  • 3,619
  • 1
  • 22
  • 35