C# 使用xsd.exe生成的架构反序列化XML时发生类型转换错误
我有一个简单的XML:C# 使用xsd.exe生成的架构反序列化XML时发生类型转换错误,c#,xml,xsd,C#,Xml,Xsd,我有一个简单的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-coor
<?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>
对于x和y坐标。
但是,我仍然有一个关于强类型和强制转换异常的错误:
+ _x_coord '(new System.Linq.SystemCore_EnumerableDebugView<NA2.room.itemRow>(this.Items.tableitem)).Items[0]._x_coord' threw an exception of type 'System.Data.StrongTypingException' int {System.Data.StrongTypingException}
+ base {System.Data.StrongTypingException: The value for column 'x-coord' in table 'item' is DBNull. ---> System.InvalidCastException: Specified cast is not valid.
+\u x_coord'(new System.Linq.SystemCore\u EnumerableDebugView(this.Items.tableitem)).Items[0]。\u x_coord'引发了类型为'System.Data.StrongTypingException'int{System.Data.StrongTypingException}的异常
+base{System.Data.StrongTypingException:表“item”中列“x-coord”的值为DBNull。-->System.InvalidCastException:指定的强制转换无效。
它只适用于xs:string和publicstringycoord。只有这样,值才会被存储而没有任何错误,但这不是我想要的
你知道问题出在哪里吗
编辑:当我从类型为int的XSD生成Room.cs时,我有int字段,但仍然会得到cast错误。我已经更改了生成的Room.c??您不会更改生成的代码。
\u x\u coord
从何而来?它是DbNull,所以它不能是Int32
。看起来它只是在命令我的int和turn把它转换成字符串。我会再看一次。看起来它仍然在试图把它变成字符串,因为我的items集合(typeof room)中coords的数据类型仍然是{System.string}System.Type{System.RuntimeType}。我不知道如何使它工作。问题似乎来自DataTable.Row,没有任何关于它的问题。
<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" />
[System.Xml.Serialization.XmlElementAttribute("y-coord", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public System.Int32 ycoord
{
get {
return this.ycoordField;
}
set {
this.ycoordField = value;
}
}
+ _x_coord '(new System.Linq.SystemCore_EnumerableDebugView<NA2.room.itemRow>(this.Items.tableitem)).Items[0]._x_coord' threw an exception of type 'System.Data.StrongTypingException' int {System.Data.StrongTypingException}
+ base {System.Data.StrongTypingException: The value for column 'x-coord' in table 'item' is DBNull. ---> System.InvalidCastException: Specified cast is not valid.