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:
Thankyou - you saved me hours :)
Thanx - You saved my neck !
-Torben
Thank you :-)
You saved my life!!!
Thanks a lot!
Another few days of someone's life saved. You're building up some serious karma...
Add one more serious thanks
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.
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...
Thanks a lot.....
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
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)
As someone else commented, you saved me hours.
Yep, following suite... thanks a ton!
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?
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!
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
Thank you great solution
That worked for me also. Thanks very much
thanks a lot
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...
Thanks Dude.
Thanks a lot. Saves lot of time.
You da man. Thanks for the post.
Thanks a lot...
you saved me..
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!
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.................
Like all others, thanks! bye, mm
really useful tip. is there a MS bug report number on that or is MS sleeping on it as usual.
Joining everybody else here - thank you SO much! So much time saved!
great!!!
Thanks very much.
Many, Many thanks!
Thanks! You solved my problem!
Legend. You just saved me all day debugging this.
Cheers!
Thanks a lot for the solution !
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;}
}
Thanks much
This is still a bug in VS 2008 almost 4 years later. Many thanks, I never would have figured this out...
..and yet again... THANKS A BUNCH!
Thanks !
Thanks a lot for sharing.
Thank you so much for posting this!!!
Many many many Thanks!!
It work like a charm for me..
Thanks
Mihai Diac
I found this post after seeing the strange error in my code at runtime and resolved it instantly with your suggestion. Thanks!
thank you so much. youre a life saver!
You're a legend!
You're a legend! Thankyou!
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
Thank you very much dude....
Thank you very much dude....
Man, it's great! Much appreciated!
I think this is the longest thank you list.here is another one...
Thank you!
Thanks a lot! good job
Seriously, you just saved me several hours research, Thanks!
Amazing. Thank you
Saved me! Good catch.
Thank You....saved me hours..
Correct solution is to change all from [][] ---> [].
5 years later and still saving people hours of work, nice work
Awesome
Great work in finding it out :)
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.
Post a Comment