RSS .92| RSS 2.0| ATOM 0.3
  • Home
  • About
  • Eric’s Toolset
  • Free Software
  • Software License
  •  

    Generating XML Serialization Assemblies

    June 13th, 2007 by Eric

    The .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:

    Project Output Dialog

    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.

    6 Responses to “Generating XML Serialization Assemblies”

    1. Eric Says:

      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

    2. Joseph Says:

      Even me I am having the same problem but mine is a web service. Kindly give me a solution

    3. Eric Says:

      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.

    4. Joseph Says:

      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’

    5. Eric Says:

      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.

    6. TechnoDex Says:

      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

    Leave a Reply