Generating XML Serialization Assemblies
June 13th, 2007 by EricThe .NET XML serialization support is pretty cool — I’ve used it for a few projects now. It provides an easy way to convert objects with properties to and from XML. The implementation dynamically generates a serialization assembly to do this at runtime, so a recommended strategy for improving runtime performance is to generate your serialization assemblies in advance using the XML Serializer Generator Tool (Sgen.exe).
I spent an embarrassingly long time coming up with the right commands for a post-build step to run sgen.exe as part of a project’s build process, but this is what I ultimately came up with:
cd "$(SolutionDir)" "$(DevEnvDir)../../SDK/v2.0/Bin/sgen.exe" /assembly:"$(TargetPath)" /parsableerrors /force /compiler:/keyfile:NextPage.snk
Then I happened to notice an item on the project’s Build tab saying “Generate serialization assembly” with a drop-down next to it, containing values “Auto”, “On” and “Off”. I started feeling kind of dumb for having spent so much time on my command line when I could presumably just pick something in the dropdown and Visual Studio would automatically figure the sgen.exe command line for me.
I thought I’d give it a try. I’m not quite sure what “Auto” means (and the documentation doesn’t really help), but surely “On” would be a reasonable choice for a project that I’m sure uses XML serialization. Woohoo! There in the output window is a call to sgen.exe! That was easy. But, um, hold on… No actual serialization assembly was generated.
There was a thread on the MSDN Forums where someone pointed out that the IDE’s version of the sgen.exe command line included the switch /proxytypes, which, according to the documentation “Generates serialization code only for the XML Web service proxy types.” Since I’m not doing a web service, the IDE’s option is mostly useless.
It is not, however, entirely useless. If you happen to be building an installer for your project in Visual Studio, turning on (that is “On”) serialization assembly generation lets installer projects pick up the serialization assemblies even if they were created using a post-build step like mine above. For the install project, you can add the project output group for XML Serialization Assemblies:

If you don’t have the “Generate serialization assemblies” option set to “On” in your main project, then the install project won’t pick up the serialization assembly, even though it is sitting there nicely in the output directory.
February 14th, 2008 at 4:57 pm
R. Aaron Zupancic points out that you can use the SGen MSBuild task to generate the serialization assembly as well:
http://blog.devstone.com/aaron/archive/2008/02/07/2778.aspx
December 31st, 2008 at 5:10 am
Even me I am having the same problem but mine is a web service. Kindly give me a solution
December 31st, 2008 at 11:40 am
Joseph, can you be more specific about the problem you’re having? In the case of a web service, it seems like using the drop-down in the project build tab would be the right approach.
January 5th, 2009 at 2:17 am
Thanks Eric.
My problem is,
Launch the server application in the server machine and than launch the web client application in the client machine (Vista). And than open a particular object in the client machine, during that time the dll of the object creates a XML serialization dynamically and stores it in the temporary folder of the client machine.
The following error occurs:
Error:
Could not find a part of the path: (which is highlighted)
C:\Users\account\AppData\Local\Microsoft\Windows\Temporary Internet Files\Virtualized\C\Users\account\AppData\Local\Temp\Low\6br1dtzk.dll’
If I Create the missing path than the following error occurs:
Error:
Could not find file:
‘C: \Users\account\AppData\Local\Microsoft\Windows\
Temporary Internet Files\Virtualized\C\Users\account\
AppData\Local\Temp\Low\6br1dtzk.dll’
January 5th, 2009 at 10:07 pm
Hmm… I haven’t encountered a problem with the dynamic generation of serialization assemblies before — that’s always seemed to work. Generating the serialization assembly in advance gives a performance benefit since it doesn’t have to be generated at run-time.
Here are a few suggestions that I can think of:
• Try generating the serialization assembly in advance either with sgen.exe or the SGen MSBuild task and see if that works.
• Use fuslogvw.exe to see if there is some unexpected dependency.
• Try describing your problem in the MSDN forums — there are lots of people more knowledgable than me.
Good luck.
January 6th, 2010 at 1:56 pm
I’ve had a problem with upgrading projects from previous version where the csproj file didn’t get the
Auto
tag added in but when looking at the Build tab of the project it showed up as Auto. If you set it to On and build then change it back to Auto and build the XmlSerializer.dll should get created