36

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?

Glenn
  • 1,955
  • 2
  • 15
  • 23

1 Answers1

73

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.

Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
  • 1
    Spot on. I was not aware of the difference between xs:int and xs:integer. I thought it was like int and Int32 in c#. – Glenn Mar 08 '12 at 10:47
  • We have the issue now when trying to send valid data to TIBCO services. Of course TIBCO isn't going to change so we have, wonderful. – reaper_unique Feb 26 '14 at 14:36
  • They definetly should use System.Numerics.BigInteger for that! – dimaaan Feb 10 '17 at 14:48