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

发布于 2024-10-31 11:36:48 字数 1662 浏览 0 评论 0原文

我正在针对 XSD.EXE 生成的类使用 XmlSerializer。

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

呕吐

InvalidOperationException 无法 生成一个临时类(结果=1)。 错误CS0030:无法转换类型 'itemOrderItemsItem[]' 到 “itemOrderItemsItem”错误 CS0029: 无法隐式转换类型 'itemOrderItemsItem' 至 'itemOrderItemsItem[]'

修复 (下面标记为 )表示要向我的架构中添加一些愚蠢的元素,但这不起作用。此修复已有五年历史。现在还有解决办法吗?

              <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...-->

I am using XmlSerializer against an XSD.EXE generated class.

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

Throws up

InvalidOperationException Unable to
generate a temporary class (result=1).
error CS0030: Cannot convert type
'itemOrderItemsItem[]' to
'itemOrderItemsItem' error CS0029:
Cannot implicitly convert type
'itemOrderItemsItem' to
'itemOrderItemsItem[]'

The fix (labeled <!--fix...--> below) says to add some silly element to my schema, but this isn't working. This fix is five years old. Is there a solution yet?

              <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...-->

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独守阴晴ぅ圆缺 2024-11-07 11:36:48

如果您有 Xsd.exe 形式的 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:

<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)-->

现在可以了。我猜序列化器只是在二维数组上吐槽。幸运的是我不需要它们。

If you have XML of the form

 <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 will generate a 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>

Then

xsd.exe "this.xsd" /c

Generates a class with two dimensional arrays (items[][]). I only wanted a one dimensional array. I changed the first line:

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

Now it works. Guess the serializer just barfs on two dimensional arrays. Luckily I dont need them.

寄离 2024-11-07 11:36:48

这为我修复了,在子元素具有 maxOccurs="unbounded" 的 xsd 文件中,我在 之后添加了额外的行:

<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-->

This fixed it for me, in xsd file where child element had maxOccurs="unbounded" I added extra line after </xs:sequence>:

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

its a known problem in XmlSerializer Code Generation component: it cannot handle some cases of nested unbounded elements. The Object Model it creates is not valid: user cannot use it to produce xml messages.

Unfortunately to fix this you have to edit your schema to make sure that all array-like constructs will be handled properly.
You would need to slightly modify all schema constructs that have the following:

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

or

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

Have to be changed to (respectively)

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

or

<xs:sequence>
  <xs:element maxOccurs="unbounded"/>
<xs:sequence>
<xs:attribute name="tmp" type="xs:string" /> <!--FIX LINE TO BE ADDED-->
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文