Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does xsd.exe generate string property for xs:integer?

Tags:

c#

.net

xsd

xsd.exe

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?

like image 812
Glenn Avatar asked Mar 08 '12 10:03

Glenn


People also ask

What is XSD integer?

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.

What is XSD EXE?

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.

Why XSD is used in C#?

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.


1 Answers

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 the xs: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.

like image 75
Enrico Campidoglio Avatar answered Oct 21 '22 19:10

Enrico Campidoglio