我有以下类(class):
[Serializable]
[XmlType(TypeName = "CoreAccount")]
public abstract class Account
{
[XmlElement(IsNullable = true)]
public string AccountNumber { get; set; }
public decimal Balance { get; set; }
public DateTime OpenDate { get; set; }
}
我正在使用 Microsoft 的 XSD.exe 从类中生成 XML 模式。在 XSD 中生成“AccountNumber”属性,如下所示:
<xs:element minOccurs="1" maxOccurs="1" name="AccountNumber" type="xs:string" />
我很高兴这个元素是必需的,但我不想接受一个空(可空)值。
我知道简单的选择是手动更新 XSD 以包括:
nillable="false"
但我希望能够以编程方式执行此操作,以便 XSD.exe 工具生成它。有人有什么建议吗?我在别处读到这是 .NET 的限制,但我想知道是否还有其他人有任何想法。
提前致谢
最佳答案
对于给定的类
[Serializable]
[XmlType(TypeName = "CoreAccount")]
public abstract class Account
{
[XmlElement(IsNullable = false)]
public string AccountNumber { get; set; }
public decimal Balance { get; set; }
public DateTime OpenDate { get; set; }
}
将始终生成具有以下元素的 xsd
<xs:element minOccurs="1" maxOccurs="1" name="AccountNumber" type="xs:string" />
这是因为 nillable
属性的默认值为 false
请参阅 - http://www.w3schools.com/xml/el_element.asp
nillable Optional. Specifies whether an explicit null value can be assigned to the element. True enables an instance of the element to have the null attribute set to true. The null attribute is defined as part of the XML Schema namespace for instances. Default is false
通过查看 XSD.exe
的文档,没有办法显式生成 xsd ( https://msdn.microsoft.com/en-us/library/x6c1kb0s(v=vs.110).aspx)
关于c# - 使用 XSD.exe 为引用类型生成 minOccurs = 1 的不可空字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21383508/