.NET Framework Tools


.NET Framework Tools 
XML Serializer Generator Tool (Sgen.exe) 

The XML Serializer Generator creates an XML serialization assembly for types in a specified assembly in order to improve the startup performance of a XmlSerializer when it serializes or deserializes objects of the specified types.

sgen [options]

Parameters

Option Description

/a[ssembly]:filename

Generates serialization code for all the types contained in the assembly or executable specified by filename. Only one file name can be provided. If this argument is repeated, the last file name is used.

/c[ompiler]:options

Specifies the options to pass to the C# compiler. All csc.exe options are supported as they are passed to the compiler. This can be used to specify that the assembly should be signed and to specify the key file.

/d[ebug]

Generates an image that can be used with a debugger.

/f[orce]

Forces the overwriting of an existing assembly of the same name. The default is false.

/help or /?

Displays command syntax and options for the tool.

/k[eep]

Suppresses the deletion of the generated source files and other temporary files after they have been compiled into the serialization assembly. This can be used to determine whether the tool is generating serialization code for a particular type.

/n[ologo]

Suppresses the display of the Microsoft startup banner.

/o[ut]:path

Specifies the directory in which to save the generated assembly.

NoteNote

The name of the generated assembly is composed of the name of the input assembly plus "xmlSerializers.dll".

/p[roxytypes]

Generates serialization code only for the XML Web service proxy types.

/r[eference]:assemblyfiles

Specifies the assemblies that are referenced by the types requiring XML serialization. Accepts multiple assembly files separated by commas.

/s[ilent]

Suppresses the display of success messages.

/t[ype]:type

Generates serialization code only for the specified type.

/v[erbose]

Displays verbose output for debugging. Lists types from the target assembly that cannot be serialized with the XmlSerializer.

/?

Displays command syntax and options for the tool.

Remarks

When the XML Serializer Generator is not used, a XmlSerializer generates serialization code and a serialization assembly for each type every time an application is run. To improve the performance of XML serialization startup, use the Sgen.exe tool to generate those assemblies the assemblies in advance. These assemblies can then be deployed with the application.

The XML Serializer Generator can also improve the performance of clients that use XML Web service proxies to communicate with servers because the serialization process will not incur a performance hit when the type is loaded the first time.

These generated assemblies cannot be used on the server side of a Web service. This tool is only for Web service clients and manual serialization scenarios.

If the assembly containing the type to serialize is named MyType.dll, then the associated serialization assembly will be named MyType.XmlSerializers.dll.

Examples

The following command creates an assembly named Data.XmlSerializers.dll for serializing all the types contained in the assembly named Data.dll.

sgen Data.dll 

The Data.XmlSerializers.dll assembly can be referenced from code that needs to serialize and deserialize the types in Data.dll.

See Also

Reference

.NET Framework Tools
SDK Command Prompt

Concepts

XML Web Services Overview



Community Content

KeithBa
Speed and security
Always use this tool. It will improve start up perf and increase security.
Tags :

Gerald Beuchelt
Understanding Serialization

This tool is very well suited to understand how XML serialization works. You need to set the /keep flag and sgen.exe will keep the (de-)serialization source code.

Modification and integration of such custom serialization code *should* be possible as well.

Tags :

amazedsaint
Not useful for a partial set of types

So how does one use sgen to serialize more than 1 but less than all types in an assembly? It ony seems to respect a single "/t:" flag. I can't quite figure out how to stitch together multiple .cs files generated using the /keep option either.

Use XGenPlus - which can generate typed serializers for a set of types - http://www.codeplex.com/xgenplus

Tags :

amazedsaint
Won't work in certain scenarios
There is a problem with Sgen. It will crash if two classes exist with same name in the source assembly, under various namespaces
Tags :

amazedsaint
A better XML Typed Serializer Generator Tool

XGenPlus is a better open source typed serializer generator tool.

XGenPlus

-> Provides a set of command line options for createing typed serializer libraries for all types or selected types in an assembly.
-> Allows programmers to create a typed serializer with out actually referreing the typed serializer library directly. For this, XGenPlus.SerializerLib can be used.
-> An MSBuild task is available to integrate XGenPlus in your Build script
-> XGenPlus is also configuration file driven.

See http://www.codeplex.com/xgenplus


DamnedYankee
sgen.exe not found - common problem solved

If you look around on the WEB you will see loads of questions regarding sgen.exe not found post-build errors. It seems that it's possible to confuse the installation of the development environment (probably by doing anything other than accepting the defaults for everything - but let's not go there).

Of course sgen.exe must be installed somewhere in order to use it. The normal way to install sgen.exe is by way of the Visual Studio SDK, In fact, if you're developing Windows Apps, there is almost no good reason to NOT install the SDK. But as I have alluded to, simply installing the SDK may not solve the issue of your post-build step failing. The reason seems to be related to how sgen.exe is found.

The location of sgen.exe is supposedly specified by a macro (AKA Environment Variable) SGenToolPath which is used within Microsoft.Common.Targets (Commonly found at C:\Windows\Microsoft.NET\Framework\%VERSION_STRING%) I have searched every file, and the registry for the definition of that string (SGenToolPath ) with no luck. Since the macro is undefined, I am guessing that a default location is used. If you have customized your installation, it is likely that that default location will be incorrect.

Since I don’t use the single gigantic C: partition - I install software (including MSDev) to a ‘user’ partition which I denominate as the ‘U:’ drive -I replace the hardcoded path to sgen within the post-build step (Project->Properties->Build Events) with this:

“$(FrameworkSDKDir)\Bin\sgen.exe”

Since the FrameworkSDKDir variable seems to be Always correctly defined, this is a safe, and portable, method of referenceing sgen.exe

I hope this helps.

Tags :

Page view tracker