XmlPreCompiler: The tool Scott Hanselman forgot

Scott Hanselman recently published the 2005 edition of his Ultimate Developer Tool List, which is a great resource for (especially Microsoft) developers. I’ve found links to a bunch of great tools I hadn’t heard about.

I was surprised at the omission of the XmlPreCompiler from the list, though. It’s not a tool that I use very often, but when I have an XML Serialization problem it’s my best ally.

Have you ever run into an exception like:

Error: System.IO.FileNotFoundException: File or assembly name k5gfdivf.dll, or one of its dependencies, was not found.

Which makes you think, “what the heck? I never created any assembly called ‘k5gfdivf’, and I think I would have remembered adding a reference to it.” Don’t worry, you’re not getting forgetful.

When you create the first instance of XmlSerializer for a type the framework dynamically generates custom C# code to map between object<->XML representations of the type. This assembly does the actual work of serializing and deserializing instances of the type. If for some reason the type to be serialized cannot be serialized correctly, the dynamically generated code may contain compilation errors. When the framework goes looking for the compiled assembly, named something like “k5gfdivf” for instance, it doesn’t find it and you get the FileNotFoundException you see above.

Microsoft has released a KB article with instructions on finding compiler errors in the code that XmlSerializer generates, but it requires some manual work. That’s where the XmlPreCompiler (or the original command line version from Chris Sells) comes into play. Run the app, select the assembly in question, and you’ll get a messagebox including the exact compilation error being encountered.

I’d rate it a must have! Maybe it will make the list in 2006?