XmlSerializer InvalidOperationExc - 转换类型时已知问题

5

我正在使用XmlSerializer针对一个由XSD.EXE生成的类进行操作。

XmlSerializer serializer = new XmlSerializer(obj.GetType());

抛出异常

InvalidOperationException 无法生成临时类(result=1)。 错误 CS0030:无法将类型“itemOrderItemsItem[]”转换为“itemOrderItemsItem”。 错误 CS0029:无法隐式转换类型“itemOrderItemsItem”为类型“itemOrderItemsItem[]”

修复方案(在下面标记为<!--fix...-->)建议在模式中添加一些无意义的元素,但这并不起作用。这个修复方案已经五年了。是否有解决方案?

              <xs:sequence>
              <xs:element name="item" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="model" type="xs:string" minOccurs="0" />
                    <xs:element name="description" type="xs:string" minOccurs="0" />
                    <xs:element name="material" type="xs:string" minOccurs="0" />
                    <xs:element name="lot" type="xs:string" minOccurs="0" />
                    <xs:element name="serial" type="xs:string" minOccurs="0" />
                    <xs:element name="transferQty" type="xs:string" minOccurs="0" />
                    <xs:element name="shipQty" type="xs:string" minOccurs="0" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>     
            </xs:sequence>
       <xs:attribute name="tmp" type="xs:string" /><!--fix...-->
2个回答

3
如果您有以下格式的XML文件:
 <items>
    <item>
      <model>10</model>
      <description>Torque wrench</description>
      <material>100</material>
      <lot>3</lot>
      <serial></serial>
      <transferQty>1</transferQty>
      <shipQty></shipQty>
    </item>
    <item>
           //...
    </item>
    <item>
           //...
    </item>
  </items>

Xsd.exe会生成一个xsd:

<xs:element name="items" minOccurs="0" maxOccurs="unbounded">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="item" minOccurs="0" maxOccurs="unbounded">
                    <xs:complexType>
                      <xs:sequence>
                        <xs:element name="model" type="xs:string" minOccurs="0" />
                        <xs:element name="description" type="xs:string" minOccurs="0" />
                        <xs:element name="material" type="xs:string" minOccurs="0" />
                        <xs:element name="lot" type="xs:string" minOccurs="0" />
                        <xs:element name="serial" type="xs:string" minOccurs="0" />
                        <xs:element name="transferQty" type="xs:string" minOccurs="0" />
                        <xs:element name="shipQty" type="xs:string" minOccurs="0" />
                      </xs:sequence>
                    </xs:complexType>
                  </xs:element>
                </xs:sequence>
              </xs:complexType>
            </xs:element>

然后

xsd.exe "this.xsd" /c

使用此命令将生成一个带有两个维度数组(items [][ ])的类。我只需要一个一维数组。我更改了第一行:

<xs:element name="items" minOccurs="0"><!--got rid of maxOccurs (which is what causes the issue)-->

现在它可以工作了。猜测序列化程序只能处理一维数组,幸好我不需要使用二维数组。

3

这对我有帮助,在子元素的xsd文件中,如果maxOccurs="unbounded",那么在</xs:sequence>后面添加一行额外的内容即可:

<xs:attribute name="tmp" type="xs:string" />

XmlSerializer代码生成组件中存在一个已知问题:它无法处理一些嵌套的未限定元素。它创建的对象模型无效,用户不能使用它来生成XML消息。

不幸的是,要修复这个问题,您需要编辑架构以确保所有类似数组的结构都能被正确处理。您需要稍微修改所有具有以下内容的架构结构:

<xs:sequence maxOccurs="unbounded">
  <xs:element ../>
<xs:sequence>

或者

<xs:sequence>
  <xs:element maxOccurs="unbounded"/>
<xs:sequence>

必须分别更改为:

<xs:sequence maxOccurs="unbounded">
  <xs:element ../>
<xs:sequence>
<xs:attribute name="tmp" type="xs:string" /> <!--FIX LINE TO BE ADDED-->

或者

<xs:sequence>
  <xs:element maxOccurs="unbounded"/>
<xs:sequence>
<xs:attribute name="tmp" type="xs:string" /> <!--FIX LINE TO BE ADDED-->

1
有一个错误报告的链接吗?似乎从.NET的开始就已经出现了这个问题,现在我们已经到了4版本,但问题仍然存在。 - AbsolutelyFreeWeb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接