【问题标题】:Use xsd to create a Nullable<int> object in generated class使用 xsd 在生成的类中创建 Nullable<int> 对象
【发布时间】:2014-10-22 01:52:14
【问题描述】:

我正在使用 xsd 创建一个 .net 类,但我似乎无法获得一个 int?作为生成类的属性之一。有没有办法做到这一点?

【问题讨论】:

  • 你试过&lt;xs:element name="myProperty" type="xs:int" xsi:nill="true"&gt;吗?
  • 你总是可以用int?属性从你的类中生成xsd,看看.NET是如何做到的。 stackoverflow.com/a/10017422/947687

标签: c# .net xsd


【解决方案1】:

xs:element设置属性nillable="true"

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-02
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 2013-04-22
    • 2020-08-06
    相关资源
    最近更新 更多