When using XmlSerializer in .Net on a self-made class, you might receive the FileNotFound exception at runtime upon instantiating the serializer. You don’t get enough info to debug this problem, and the filename of the file it didn’t find can’t explain much either, because the dll-name is different ‘garbage’ every time.

XmlSerializerPreCompiler to the rescue

Chris Sells has developed the XmlSerializerPreCompiler tool, which gives you more info about why instantiating an XmlSerializer for your class won’t fly.

The XmlSerializer PreCompiler is a command-line program that performs the same steps as the constructor of the XmlSerializer. It reflects over a type, generates serialization classes, and compiles them. Any compilation errors are written to the console.

The tool is very easy to use. You simply point the tool at the assembly that contains the type that causes the exception, and specify which type to pre-compile. But it gets even better! Mathew Nolton has written a GUI for the tool, so using it is even easier. A link to the graphical version of the XmlSerializerPreCompiler can be found at Chris Sells’ site.

Solving the issue

The tool finally told me the XmlSerializer couldn’t be instantiated because of a test-change I made: I made an inner class private, because I added an .Add method for the collection of that innerclass in the main class. That won’t fly during serialization.