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

C#XsdXml Deserialization

C# Problem Overview


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

C# Solutions


Solution 1 - C#

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

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

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

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

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

Solution 6 - C#

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

Solution 7 - C#

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!

Solution 8 - C#

This is really @WaldemarGałęzinowski's answer https://stackoverflow.com/a/35896991/157224 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]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongradyView Question on Stackoverflow
Solution 1 - C#AjaxView Answer on Stackoverflow
Solution 2 - C#classicskidsView Answer on Stackoverflow
Solution 3 - C#shaishav shuklaView Answer on Stackoverflow
Solution 4 - C#Waldemar GałęzinowskiView Answer on Stackoverflow
Solution 5 - C#Leendert NelemansView Answer on Stackoverflow
Solution 6 - C#JB9View Answer on Stackoverflow
Solution 7 - C#Ashraf AlamView Answer on Stackoverflow
Solution 8 - C#adrianmView Answer on Stackoverflow