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

8

54

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

Maggio answered 13/7, 2011 at 12:33 Comment(3)
Did you see the discussion here: social.msdn.microsoft.com/forums/en-US/asmxandxml/thread/… it seems you can add a dummy attribute to your xsd. Not sure if this is the problem you are facing.Leslee
I am having a simular problem, but adding the dummy attribute didn't fix it, see my question here: https://mcmap.net/q/298141/-how-can-i-correctly-modify-a-generated-xsd-to-overcome-a-known-net-bug-that-causes-exception-quot-cs0030-unable-to-generate-a-temporary-class-quot/198048Wollastonite
This is outdated, the blog link has been locked..Globate
I
130

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

Istanbul answered 17/10, 2012 at 4:57 Comment(11)
Thanks for this hint. But why does the Microsoft XSD Tool generates 2D arrays?Escarpment
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...!Kreiner
When I ran into this issue, I regenerated the schema with xsd2Code and that fixed the issue.Ehrenberg
Why hasn't this been fixed yet, just had the same problem over 2 years laterCressi
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.Sikorsky
Same issue. This fixed it. Thanks.Squiggle
And again in 2018Labyrinth
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 crazyOrthman
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.Spook
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.Endarch
And again in 2020. Thank you!Variscite
P
17

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.

Plasmodium answered 9/12, 2014 at 10:28 Comment(0)
L
9

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

Laughry answered 10/5, 2016 at 9:10 Comment(1)
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.Workbag
C
6

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.

Catchpole answered 9/3, 2016 at 16:19 Comment(2)
Can you please give an example of this?Petition
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" />Facilitate
P
3

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

Pase answered 7/4, 2017 at 14:24 Comment(0)
S
1

If its in VB.net then you got to search for ()() in your Reference.vb and replace with()

Surprint answered 1/3, 2016 at 21:24 Comment(0)
V
0

For my case, the issue cases due to an invalid declaration for XmlArrayItem property attribute.

From

[XmlArrayItem("MyArray", typeof(string))]
public List<ClassName> Items{ get; set; }

I changed with appropriate type: from string to ClassName

[XmlArrayItem("MyArray", typeof(ClassName))]
public List<ClassName> Items{ get; set; }

Hope this helps!

Viridis answered 23/6, 2015 at 10:55 Comment(0)
I
0

This is really @WaldemarGałęzinowski's answer https://mcmap.net/q/297333/-unable-to-generate-a-temporary-class-result-1-error-cs0030-cannot-convert-type-39-type-39-to-39-type-39 expanded a bit.

xsd.exe has an optimization that kicks in when you have a single unbounded element without attribute in a sequence.

Parent-Child

The optimization will avoid creating a special type for the parent and instead make it an array of children.

ChildType[] Parent { get; set; } instead of ParentType Parent { get; set; } and you access the children like Parent[0] instead of Parent.Child[0]. (I find this optimization a bit confusing sometimes)

What is happening here is you have one more level of unbounded, attribute-less element

Parent-child-grandchild

The optimization is applied twice and the result is GrandChildType[][] Parent {get; set;} and you access your favorite first grandchild like Parent[0][0] instead of Parent.Child[0].GrandChild[0].

The problem is the .Net serializer does not support arrays of arrays and generates invalid code.

I have no idea why Microsoft has not fixed this bug in all these years but the workaround is simple.

Just force xsd.exe to generate a class for the parent or child by adding an optional attribute or an optional element to the sequence. e.g. workaround

Which leads to Parent[0].GrandChild[0]

Inspissate answered 4/2, 2021 at 7:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.