There was an error while deserializing intermediate XML. Cannot find type “namespace.class”
I have been staring at this error for a few days now. It’s been doing my head in.
It all started when I decided to take a brake from my utility app that I have been writing for Windows Phone now for quite a while, so I thought I’d start writing a game instead. I wanted to make some of the text handling easier so I decided to try out the new combined Silverlight XNA mode for the Mango release of the Windows Phone SDK.
I have never coded any XNA apps before but getting the basics up, importing textures from the content pipeline and such went fairly ok.
Then I wanted to add XML files to describe my game objects and use the content pipeline to serialize those to objects. This is where the headache started…
The project ‘SlXnaApp1Lib’ cannot be referenced
This error pops up when you create the project from scratch, but it is described in the release notes and said to be ok:
The default 3D Graphics Application project template contains the compiler warning “The project <ProjectName> cannot be referenced. The referenced project is targeted to a different framework family (.NETFramework).” This warning does not affect your application and should be left in place.
So apparently that’s something we have to live with. This made me assume I could not put code in the Game Library project, so I decided to put the class I was deserializing to in the main project. But this just kept getting me the error that “there was an error deserializing intermediate xml”.
After scouring the web for information it seemed clear that the content library is built before the main project is, so it’s not possible to put the class definitions for deserialization in the main project. Yay…
Trying to move class definitions into the Game Library
Ok, so moving classes into the Game Library seemed the only route. But just moving the class in question into the Game Library still produced the same errors.
It seems the Content Library project is not aware of the Game Library project, and hence can’t get to the class definition. So just adding a reference should be easy and everything should be sorted!
That didn’t work at all since it creates a circular dependency.
At this point I’m starting to wonder if it’s my installation of the Windows Phone SDK that’s broken, so I deinstalled everythign and then reinstalled the Windows Phone SDK 7.1 RC. But this issue is still the same. So maybe is it the tools that are broken?
In the simple examples showing off the Silverlight / XNA template they aren’t deserializing any XML files, so there seem to be no real example from Microsoft available either. The more advanced examples using deserialization isn’t using the Silverlight / XNA template, but the approach of adding the class definitions for deserialization in the Game Library is exactly what they do.
Getting things to work
As I want to continue coding on my game I felt I wanted to solve this issue even if the tools aren’t ready for it.
So I found a way, even though I suspect it’s a hack and will end up counterproductive in the end for the project.
What I did was:
1. Add a reference straight to the GameLib dll. Problem here is that it’s not even getting built at this stage.
Problem is that at this moment the dll isn’t even being built! The XML deserializer is failing the build since it can’t find the class definition it’s looking for. So, removing the XML file seems appropriate.
After removing the XML file the project builds. Now it’s possible to add a reference to the dll of the Game Lib to the Content Lib project.
2. Now we can put the XML file back into the Content Lib and build the solution.
3. Putting a debug breakpoint and looking at the deserialized object shows it works finally!!!
(Always remember to add an empty constructor to classes that’s going to be serialized with .NET!)
I think this is a messy solution and it shouldn’t work like this. I’m especially scared of that I have to go through this rinse-rebuild-add cycle if I add new XML definitions and get the class definitions wrong from the getgo.
I suspect there is a simpler solution out there that I just didn’t manage to find. So if any one is sitting on the proper solution, please – oh please let me know of it!