Wednesday, December 20, 2006

xsd.exe generated classes causing "Unable to generate a temporary class" exception


This has been an excruciatingly hard problem to fix. Maybe I can get the solution a little bit more exposure if I post it here too, finding it myself was a hell.

The error is triggered when you try to create an instance of a XmlSerializer with a parameter type of a class generated from an xsd with xsd.exe.

I'm working on .net2.0, a class library project. Many of the answers to the query in the title on Google head you to ASP permission problems, witch seams to be just a false lead, and an entirely different matter.

If you get array related stuff like:
Unable to generate a temporary class (result=1).
error CS0030: Cannot convert type 'Data[]' to 'Data'
error CS0029: Cannot implicitly convert type 'Data' to 'Data[]'

read on, otherwise you might be facing something different.

The problem is the xsd.exe tool has a bug when it comes to mapping to a multidimensional array. In most cases you can easily fix it by hand.

In my case, 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


As it can be seen, the type of the elements of a Data[][] array is not Data, but Data[]. The only change required was:
[System.Xml.Serialization.XmlArrayItemAttribute("Data", typeof(Data[]), IsNullable=false)]
public Data[][] Row


Happy coding, and hopefully we'll be spared of this kind of framework bugs in the future.

62 comments:

Urmila said...

Thankyou - you saved me hours :)

Anonymous said...

Thanx - You saved my neck !

-Torben

Anonymous said...

Thank you :-)

Felipe said...

You saved my life!!!
Thanks a lot!

Anonymous said...

Another few days of someone's life saved. You're building up some serious karma...

Anonymous said...

Add one more serious thanks

satov said...

Very very glad to be of help :).

Still it is an ugly hack. If any of you find a ms patch or something, please return and post it here too.

chris wright said...

Hey Man, you saved my bacon..after spending a good few hours trying to get this to work...starting my new job tomorrow and needed to understand xml serialization...

Anonymous said...

Thanks a lot.....

Anonymous said...

adding a dumb attribute will force the XmlSerializer code Generator to use a class with array members and proper XmlSerialization attributes for the construct instead of an unwrapped array.
MSDN Post

BB said...

Thanks for the posting.
Following your direction, I am able to deserialize the xml.

I am having problem is to get the element content of the multi dimentional array from the deserialize xml?
( Data[][] in your case)

Anonymous said...

As someone else commented, you saved me hours.

jandersen said...

Yep, following suite... thanks a ton!

Anonymous said...

BB you mentioned that you’re having problems getting the data from the deserialized XML. I’m experiencing that too!

It seems that the 2’nd level array is always empty… has anybody experienced that and maybe found a solution?

Dolph said...

been fighting with this one for a while, great fix.. I've also read that removing the maxoccurs works as well. I didn't want to do this though as it's a failure on the xsd.exe's part. Thanks bud, owe you a beer!

dilpreet said...

This is great findings. I used XSDObjectGenerator to generate a class. Got similar problem and I was I tried everything but nothing worked and at last I got this life saving link. Thanks

Anonymous said...

Thank you great solution

ramon said...

That worked for me also. Thanks very much

Naveen Hegde said...

thanks a lot

Anonymous said...

thanks a ton ...nice work around....but i had a problem after Changing the datatype ....the code was executing without a glitch...but the object had soem data missing ....that was a serious problem ...i had to work around and figured out that there is a better way to solve this rather than using "[]" as suggested here ...instead of declaring them as arrays declare them as object and it would solve the probem without compromising On the data...

Loopsludge said...

Thanks Dude.

Anonymous said...

Thanks a lot. Saves lot of time.

Ken said...

You da man. Thanks for the post.

Anonymous said...

Thanks a lot...
you saved me..

Anonymous said...

Seriously Dude - you get 10 out of 10 for saving many people so much time.It's been said already but bears repeating - YOU ARE DA MAN!

debbyxx94 said...

thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks,thanks.................

Anonymous said...

Like all others, thanks! bye, mm

Anonymous said...

really useful tip. is there a MS bug report number on that or is MS sleeping on it as usual.

Anonymous said...

Joining everybody else here - thank you SO much! So much time saved!

Raúl Andrés Duque said...

great!!!
Thanks very much.

Anonymous said...

Many, Many thanks!

Anonymous said...

Thanks! You solved my problem!

Gareth said...

Legend. You just saved me all day debugging this.

Cheers!

Anonymous said...

Thanks a lot for the solution !

Jonas said...

Thanks a lot, your post was very helpful.

By default, XSD generate bidimensional arrays. I converted themo to unidimensional array and it all became mor easy...

Data[] dataField;

[System.Xml.Serialization.XmlArrayItemAttribute("data", typeof(Data), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public Data[] data {
get { return this.gadgetsField; }
set {this.gadgetsField = value;}
}

Anonymous said...

Thanks much

Kryten said...

This is still a bug in VS 2008 almost 4 years later. Many thanks, I never would have figured this out...

David A Brown said...

..and yet again... THANKS A BUNCH!

Anonymous said...

Thanks !

SM4JL said...

Thanks a lot for sharing.

Anonymous said...

Thank you so much for posting this!!!

Anonymous said...

Many many many Thanks!!

Anonymous said...

It work like a charm for me..
Thanks
Mihai Diac

Anonymous said...

I found this post after seeing the strange error in my code at runtime and resolved it instantly with your suggestion. Thanks!

Anonymous said...

thank you so much. youre a life saver!

delomar said...

You're a legend!

delomar said...

You're a legend! Thankyou!

Anonymous said...

Just one more thanks. I ended up being stuck for only 30 seconds because your article came up as the first hit to my search.

Kim

Anonymous said...

Thank you very much dude....

Anonymous said...

Thank you very much dude....

Anonymous said...

Man, it's great! Much appreciated!

Anonymous said...

I think this is the longest thank you list.here is another one...
Thank you!

Anonymous said...

Thanks a lot! good job

DotNetBlocks.com said...

Seriously, you just saved me several hours research, Thanks!

P8TRO said...

Amazing. Thank you

Anonymous said...

Saved me! Good catch.

Techman said...

Thank You....saved me hours..

Ojas said...

Correct solution is to change all from [][] ---> [].

Anonymous said...

5 years later and still saving people hours of work, nice work

Anonymous said...

Awesome

Anonymous said...

Great work in finding it out :)

Kumanan Murugesan said...

Thank you very much. Assomebody has said the second level array is always empty, I too had the similar problem. Again the solution was also posted in the comments. Converting them to uni dimentional array solved my problem.