Coder Perfect

The constructor of the XmlSerializer throws a FileNotFoundException.

Problem

When I try to serialize types in an application I’ve been working on, it fails.

A statement like

XmlSerializer lizer = new XmlSerializer(typeof(MyType));

produces:

System.IO.FileNotFoundException occurred
  Message="Could not load file or assembly '[Containing Assembly of MyType].XmlSerializers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified."
  Source="mscorlib"
  FileName="[Containing Assembly of MyType].XmlSerializers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
  FusionLog=""
  StackTrace:
       at System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
       at System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)

For my class, I don’t define any specific serializers.

What can I do to resolve this issue?

Asked by Irwin

Solution #1

This is, believe it or not, typical behavior. The XmlSerializer throws an exception, but it’s handled by it, so if you just ignore it, everything should be good.

This has irritated me greatly, and there have been numerous complaints about it if you search around a bit, but from what I’ve read, Microsoft has no plans to address it.

If you turn off first chance exceptions for that specific exception, you can prevent getting Exception popups all the time when debugging. In Visual Studio, go to Debug -> Exceptions (or press Ctrl + Alt + E), Common Language Runtime Exceptions -> System.IO -> System.IO.FileNotFoundException, Common Language Runtime Exceptions -> System.IO -> System.IO.FileNotFoundException.

Another solution is discussed in the blog post C# XmlSerializer FileNotFound exception (which also mentions Chris Sells’ tool XmlSerializerPreCompiler).

Answered by Martin Sherburn

Solution #2

This is normal behaviour, as Martin Sherburn pointed out. The XmlSerializer’s function Object() { [native code] } looks for an assembly named [YourAssembly] first. XmlSerializers.dll, which should contain the serialisation class for your type. A FileNotFoundException is issued because such a DLL has not yet been built (they are not by default). When this occurs, the XmlSerializer’s function Object() { [native code] } catches the exception, and the DLL is built automatically at runtime by the XmlSerializer’s function Object() { [native code] } (by generating C# source files in your computer’s temp% directory, then building them with the C# compiler). Additional XmlSerializer constructs for the same type will simply use the DLL that has already been created.

The constructor of the XmlSerializer handles the exception. There is no need for you to do anything; simply press the ‘Continue’ (F5) key to continue running your software, and everything will be OK. You either have ‘Just My Code’ turned off, or the FileNotFoundException is set to halt execution when thrown, rather than when ‘User-unhandled’, if you’re bothered by errors stopping your program’s execution and bringing up an exception helper.

Go to Tools >> Options >> Debugging >> General >> Just My Code to enable it. Just My Code should be enabled. Go to Debug >> Exceptions >> FileNotFound to disable execution breaking when FileNotFound is thrown. Find >> type ‘FileNotFoundException’ into the search box >> uncheck the ‘Throwed’ option in System.IO. FileNotFoundException.

Answered by Allon Guralnek

Solution #3

There is a “create serialisation assembly” option in the Visual Studio project properties (“Build” tab, if I recall correctly). For a project that generates [Containing Assembly of MyType], try turning it on.

Answered by VladV

Solution #4

There is a way to get around this. If you make use of

XmlSerializer lizer = XmlSerializer.FromTypes(new[] { typeof(MyType) })[0];

That exception should be avoided. This was effective for me.

WARNING: If you use it more than once, you’ll get a memory leak.

If you use this way to generate many instances of XmlSerializer for the same type, you’ll waste a lot of RAM!

This is because the built-in caching given by the XmlSerializer(type) and XmlSerializer(type, defaultNameSpace) constructors is bypassed by this technique (all other constructors also bypass the cache).

If you don’t utilize these two constructors to create an XmlSerializer, you’ll have to write your own caching or you’ll run out of memory.

Answered by quadfinity

Solution #5

I had the same problem and was unable to solve it using any of the suggested techniques.

Then I came up with a solution. The serializer appears to require not only the type, but also the nested types. Changing the situation:

XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

To this:

XmlSerializer xmlSerializer = new XmlSerializer(typeof(T).GetNestedTypes());

I was able to resolve the issue. There will be no more exceptions.

Answered by Frosty

Post is based on https://stackoverflow.com/questions/1127431/xmlserializer-giving-filenotfoundexception-at-constructor