Sunday 15 September 2013

c# - Error with type cast while deserializing XML using xsd.exe generated schema -



c# - Error with type cast while deserializing XML using xsd.exe generated schema -

i have simple xml:

<?xml version="1.0" encoding="utf-8"?> <room> <item type="computer"> <x-coord>1</x-coord> <y-coord>2</y-coord> </item> <item type="chair"> <x-coord>4</x-coord> <y-coord>5</y-coord> </item> </room>

and i've generated xml schema it, follows:

<?xml version="1.0" encoding="utf-8"?> <xs:schema id="room" xmlns="" xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <xs:element name="room" msdata:isdataset="true" msdata:locale="en-us"> <xs:complextype> <xs:choice minoccurs="0" maxoccurs="unbounded"> <xs:element name="item"> <xs:complextype> <xs:sequence> <xs:element name="x-coord" type="xs:string" minoccurs="0" msdata:ordinal="0" /> <xs:element name="y-coord" type="xs:string" minoccurs="0" msdata:ordinal="1" /> </xs:sequence> <xs:attribute name="type" type="xs:string" /> </xs:complextype> </xs:element> </xs:choice> </xs:complextype> </xs:element> </xs:schema>

because want x-coord , y-coord fields hold integer value, i've changed xs:string xs:int, this:

<xs:element name="x-coord" type="xs:int" minoccurs="0" msdata:ordinal="0" /> <xs:element name="y-coord" type="xs:int" minoccurs="0" msdata:ordinal="1" />

i've read somewhere should hold int32 value, i've changed generated room.cs have:

[system.xml.serialization.xmlelementattribute("y-coord", form=system.xml.schema.xmlschemaform.unqualified)] public system.int32 ycoord { { homecoming this.ycoordfield; } set { this.ycoordfield = value; } }

for both x , y coordinate. however, still have error strong types , cast exception:

+ _x_coord '(new system.linq.systemcore_enumerabledebugview<na2.room.itemrow>(this.items.tableitem)).items[0]._x_coord' threw exception of type 'system.data.strongtypingexception' int {system.data.strongtypingexception} + base of operations {system.data.strongtypingexception: value column 'x-coord' in table 'item' dbnull. ---> system.invalidcastexception: specified cast not valid.

it works xs:string , public string ycoord. values stored without error not want.

do know problem here?

edit: when generate room.cs xsd type int, have int fields , still cast error.

c# xml xsd

No comments:

Post a Comment