Unable to generate a temporary class (result=1). error CS0030: Cannot convert type 'Type[]' to 'Type'?

43,762

Solution 1

You need to change the type of a member variable in the serialized class. For example if its raising an error like:

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

I ran a search on the Data type name in the generated file, and I found this:

[System.Xml.Serialization.XmlArrayItemAttribute("Data", typeof(Data), IsNullable=false)]
public Data[][] Row

Replace Data[][] with Data[] - Change the type of Data from a 2D array to a 1D array. It would solve your problem. :)

Solution 2

Had the same problem, but Xsd2Code didn't integrate with VS2012. So instead I went to my xsd.exe generated .cs file and did:

Find [][] Replace []

which worked.

Solution 3

I got this error.In your solution there is reference.cs file in that file you need to search "[][]" and then there will be two results in it..

After you need to remove one "[]" from "[][]" from both places..

It works for me..

Thanks..

Solution 4

Add <xs:attribute name="tmp" type="xs:string" /> after every
<xs:sequence maxOccurs="unbounded"> <xs:element ../> </xs:sequence>
and
<xs:sequence> <xs:element maxOccurs="unbounded"/> </xs:sequence>
element in your schema file if you don't want to loose dimension of the array.

Solution 5

For me it helps to patch the XML used to generate the code. It happens when:

<Names>
    <Name></Name>
    <Name></Name>
</Names>

then this is optimized by xsd to double array name entry

What I did is:

<Names>
    <Dummy></Dummy>
    <Name></Name>
    <Name></Name>
</Names>

the xsd doesn't optimize it but leaves the single array name

Share:
43,762
grady
Author by

grady

Updated on July 17, 2022

Comments

  • grady
    grady almost 2 years

    I get this error after I created a class from my xsd file using the xsd.exe tool. So I searched the net and found a solution. Here is the link: http://satov.blogspot.com/2006/12/xsdexe-generated-classes-causing.html

    Problem is that this makes the code run, but somehow the deserialized data seems corrupt. I did what the site suggests and in the end the 2nd array dimension is always empty (see the comments of the site, somebody also had this problem). Question is, how do I solve this issue now? Is there another tool to create the xsd file? I tried Xsd2Code, without success.

    Thanks :-)

  • float
    float about 11 years
    Thanks for this hint. But why does the Microsoft XSD Tool generates 2D arrays?
  • Code Monkey
    Code Monkey almost 11 years
    I had the same issue while using Microsoft's XSD to generate a class for the XML returned by TD Ameritrade's stock broker API.. I wonder if there are tools better than XSD.EXE that auto-generate without us needing to manually clean up the C# class? I am getting too many errors in a single XML and I have many more to process...!
  • blachniet
    blachniet about 10 years
    When I ran into this issue, I regenerated the schema with xsd2Code and that fixed the issue.
  • Coops
    Coops over 9 years
    Why hasn't this been fixed yet, just had the same problem over 2 years later
  • yantaq
    yantaq over 8 years
    I encountered same error when serializing the XSD.EXE generated type instance and error is fixed after change 2D array to 1D array in XSD.EXE generated class Thanks.
  • Eric
    Eric over 8 years
    Same issue. This fixed it. Thanks.
  • Najera
    Najera over 7 years
    Can you please give an example of this?
  • nivs1978
    nivs1978 about 7 years
    Of all the answers, this is the one who works for me. Seems Visual Studio, when you add a service reference, makes this very mistake.
  • Piero Alberto
    Piero Alberto almost 6 years
    And again in 2018
  • Simon Price
    Simon Price over 5 years
    Nope... still an issue... unless FedEx are running on the same thing for YEARS... this has just been a life saver as I was going crazy
  • Scott Salyer
    Scott Salyer almost 5 years
    Still not fixed - I just generated a new class file from an XSD and thankfully SO saved me (as usual), because this was really confusing.
  • joe_maya
    joe_maya almost 5 years
    Even though the solution provided here prevents the exception from being thrown, it doesn't necessarily solve the issue in my case. It leaves out some data from the XML that I am trying to parse. Specifically, the conversion from 2d to 1d causes only the first item to be picked up.
  • Nicolás Abram
    Nicolás Abram over 4 years
    And again in 2020. Thank you!
  • bazza
    bazza about 2 years
    This worked a treat, thank you very much. @Najera, you simply place the line <xs:attribute name="tmp" type="xs:string" /> following on from the end of the sequence, e.g. </xs:sequence> <xs:attribute name="tmp" type="xs:string" />