简体   繁体   中英

Using XSD.exe to generate non-nillable field with minOccurs = 1 for reference type

I have the following 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; }
}

I am using Microsoft's XSD.exe to generate a XML schema from the class. The 'AccountNumber' property is being generated in the XSD as follows:

<xs:element minOccurs="1" maxOccurs="1" name="AccountNumber" type="xs:string" />

I am happy that this element is required but I do want to accept an empty (nillable) value. 希望接受一个空的(的nillable)值。

I know that the easy option is to manually update the XSD to include:

nillable="false"

But I want to be able to do this programmatically so the XSD.exe tool will generate this. Does anybody have any suggestions? I have read elsewhere that this is a limitation of .NET but am wondering if anybody else has any ideas.

Thanks in advance

For the given class

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

Will always produce an xsd with the following element

<xs:element minOccurs="1" maxOccurs="1" name="AccountNumber" type="xs:string" />

This is because the default value for the nillable attribute is false See - 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

From looking at the documentation of XSD.exe there isn't a way of explicitly generating the xsd ( https://msdn.microsoft.com/en-us/library/x6c1kb0s(v=vs.110).aspx )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM