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...-->
Since XmlSerializer is one of the few thread safe classes in the framework you really only need a single instance of each serializer even in a multithreaded application.
The XmlSerializer creates C# (. cs) files and compiles them into . dll files in the directory named by the TEMP environment variable; serialization occurs with those DLLs. These serialization assemblies can be generated in advance and signed by using the SGen.exe tool.
Serialization/ De-serialization allow communication with another application by sending and receiving data. With XmlSerializer, you can control how objects are encoded into XML. Call the Serialize method with the parameters of the StreamWriter and object to serialize.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With