When I generate a c# class from a xsd schema with xsd.exe I find this behaivor a bit wierd.
My element:
<xs:element name="InvoiceNo" type="xs:integer"/>
is generated to:
[System.Xml.Serialization.XmlElementAttribute(DataType="integer", Order=5)]
public string InvoiceNo
{
...
}
Why is that property not generated as an int instead of string?
Description. The value space of xsd:int is the set of common single-size integers (32 bits), the integers between -2147483648 and 2147483647. Its lexical space allows any number of insignificant leading zeros.
The XML Schema Definition (Xsd.exe) tool generates XML schema or common language runtime classes from XDR, XML, and XSD files, or from classes in a runtime assembly.
XSD is a schema language; you use it to define the possible structure and contents of an XML format. A validating parser can then check whether an XML instance document conforms to an XSD schema or a set of schemas.
This behavior is by design:
The
xs:integer
type is specified as a number with no upper or lower bound on its size. For this reason, neither XML serialization nor validation map it to the System.Int32 type. Instead, XML serialization maps thexs:integer
to a string while validation maps it to the Decimal type that is much larger than any of the integer types in the .NET Framework
Use xs:int
, which is a signed 32-bit integer, to have Xsd.exe map it to a System.Int32:
<xs:element name="InvoiceNo" type="xs:int" />
Here's a detailed list of the data types defined in the XML Schema Definition standard.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With