2

Can anyone tell me, why integer elements in xsd are being converted in String fields?

<xs:element name="OwnerID" type="xs:integer"/>

into

[System.Xml.Serialization.XmlElementAttribute(DataType = "integer")]
public string OwnerID
{
    get
    {
        return this.ownerIDField;
    }
    set
    {
        this.ownerIDField = value;
    }
}

my first assumption that all fields are strings attributed by real datatype was not right - dates are interpretered as dates, bools as bools - what's wrong with integer?

Thanks in advance!

sq33G
  • 3,320
  • 1
  • 23
  • 38
Andrey Khataev
  • 1,303
  • 6
  • 20
  • 46

2 Answers2

4

You need something like this:

<xs:element name="OwnerID" >
    <xs:simpleType>
        <xs:restriction base="xs:int" />
    </xs:simpleType>
</xs:element>
sq33G
  • 3,320
  • 1
  • 23
  • 38
2

sq33G's answer is correct, but I want to add that the reason your original XSD element

<xs:element name="OwnerID" type="xs:integer"/>

is converted to a string is because per W3C Numeric DataTypes, xs:interger represents any integer value. Since is not confined to a 32 or 64-bit number and there is no numeric data type in C# that can handle an unbounded integer, the Deserializer is choosing a string type since it is the only type that can safely handle this value.

psubsee2003
  • 8,563
  • 8
  • 61
  • 79
  • I need to correct myself - i remembered the behavior incorrectly. xs:integer does map to the System.Decimal type – psubsee2003 Nov 06 '11 at 08:47