title: XmlSerializer - the first deserialization is very slow
body: <p>I have a solution with two projects; an asp.net MVC application, and a class library. Let's call them project MVC and project CLS.</p>
  
  <p>In the project CLS, there are two different versions (V1 and V2) of an XSD file that I have used to create two serializable classes with the same name, but under different namespaces (V1 and V2) using <a href="http://xsd2code.com/" rel="noreferrer">xsd2code</a>.</p>
  
  <p>In the MVC project, when the user uploads an XML file, the <code>CLS.dll</code> is used to deserialize the XML into an object. When the XML file is of type V1, the deserialization is very fast, but the XSD file for the V2 version is a lot more complex, and the deserialization can take up to a couple of minutes, only the first time (it's very fast afterwards, until the application is run again).</p>
  
  <p>I used the <a href="https://docs.microsoft.com/en-us/dotnet/standard/serialization/xml-serializer-generator-tool-sgen-exe" rel="noreferrer">Sgen.exe</a> tool to create a serializer assembly (<code>CLS.XmlSerializers.dll</code>) for the CLS.V2 type in order to eliminate the first-time creation of the assembly on the fly, and therefore improving the performance.</p>
  
  <p>I have successfully managed to add the Sgen Task to the Post Build events, and the assembly <code>CLS.XmlSerializers.dll</code> is created every time I build the project. Also, I have used the unit test code in <a href="https://stackoverflow.com/questions/10512299/xmlserializer-not-using-xmlserializers-dll-created-by-sgen">this post</a> to make sure the assembly is loaded, and it does. The test passes susscessfully. </p>
  
  <p>However, still, the first time the XML file is deserialized, it takes a long time. So, something still should be wrong. But, I don't know what. Please help.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I used <code>Fuslogvw.exe</code> as was suggested in the comments, and I can see that the <code>CLS.XmlSerializers.dll</code> is being loaded successfully. Then, how come the first time the XML file is deserialized it takes around one minute, but every time after that takes less than a second?</p>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p>One of the differences between the two XSD files is that the second one (V2) has a reference to a very big XSD file that containes definitions of some <code>xs:enumeration</code> types that are used in the main file. And, that's the reason the deserialization took a long time. Since all I need to do is to deserialize the XML files into objects and do not need to validate the values of the attributes and elements against those enumerations, I ended up removing the reference to that XSD file, and replacing all the enumeration types with their base types (in this case, <code>xs:string</code>). Now, V2 is deserialized as fast as V1, and I don't even need to use <code>Sgen.exe</code>. I guess <code>Sgen.exe</code> only helps in situations where you need to deserialize a very large XML file. In my case, the XML files are always very small, but the desrialization is (was) complex.</p>