开发者

XmlSerializer InvalidOperationExc - known issue converting types

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

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

Throws up

InvalidOperationException Unable to generate 开发者_StackOverflow社区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...-->


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜