This article describes step by step optimizing XML Serialization using SGEN.exe utility provided by Microsoft in .Net SDK
Introduction
This article describes step by step optimizing XML Serialization using SGEN.exe utility provided by Microsoft in .Net SDK. I have recently implemented this in of my project to fix a major performance issue where a transaction time reduced from 10 seconds to 2 second.I hope this will be useful.
Advantage of SGEN.EXE in .Net XML serialization:
Whenever we use XMLSerializer.Serialize(),by default, at runtime, .Net will generate a Type Serializer class on the fly, compile it using C# compiler into a temporary assembly, load the assembly and then use the generated class to serialize ,which makes the process slow. We can use sgen.exe to create a persistent Type Serializer assembly and prevent .Net to generate temporary assembly every time we serialize.
Step by Step using SGEN.exe to serialize.
In the below example I am trying to serialize a object using SGEN.exe.
1. Create a new class library with below code and compile it to generate DataToSerialize.dll
namespace
DataToSerialize
{
public class Person
{
public string Name;
public int Age;
}
}
2. Go to Visual Studio command prompt, point it to the bin folder where DataToSerialize.dll reside and then enter the below command.
C:\Program Files\Microsoft Visual Studio 8\VC>sgen "(appPath)\bin\Debug\DataToSerialize.dll"
This will generate DataToSerialize.XMLSerializer.dll in the bin\Debug folder.

3. Create a new Console Application.
4. Add reference to both DataToSerialize.dll & DataToSerialize.XMLSerializer.dll
5. Import below namespaces.
using
System.Text;
using System.IO;
using
DataToSerialize;
using
Microsoft.Xml.Serialization.GeneratedAssembly;
6.In the main method write the below code:
//1.Build the
object you want to serialize
Person p = new
Person();
p.Age = 29;
p.Name = "Satya
Narayan Sahoo";
//2.Create a
string builder and string writer to hold the serialize XML.
StringBuilder
buildr = new StringBuilder();
StringWriter
writr = new System.IO.StringWriter(buildr);
//3.Person
serializer is the autogenerated type serializer for serializing Person object.
PersonSerializer mySerialzer = new PersonSerializer();
mySerialzer.Serialize(writr, p);
//4.Print the
XML from the string writer.
string str =
writr.ToString();
Console.WriteLine(str);
//5.Deserialize
and get back the object.
StringReader
t=new StringReader(str);
Person objP =
(Person)mySerialzer.Deserialize(t);
7. Now run the program you should be able to see the serialized value of the object o.
I have attached all the source for this example as SgenSerialization1.zip.

Comparison of Performance:
This way of serialization is much more faster than the normal XML Serialization .Incase of complex and heavy object you can see a incredible performance difference .I will show the same in the below example. Here I have added a hardcoded dataset to the person class to make the object heavy and then serializing the person object using normal XMLSerialization and SGENSerialization. Time taken to serialize in both case are printed to console.
I have attached all the code for performance comparison as SgenSerialization.zip. Below are the explanation of the code.
1-Person class has been modified and a hardcoded dataset is added to the class to make the Person object heavier.
namespace
DataToSerialize
{
public class Person
{
public string Name;
public int Age;
public DataSet ds;
public
Person()
{
ds = new DataSet();
for (int i = 0; i < 10; i++)
{
DataTable
tbl = new DataTable("Table" + i.ToString());
tbl.Columns.Add("FistName", typeof(string));
tbl.Columns.Add("LastName", typeof(string));
tbl.Columns.Add("MiddleName", typeof(string));
tbl.Columns.Add("FatherFirstName", typeof(string)); tbl.Columns.Add("FatherLastName", typeof(string)); tbl.Columns.Add("FatherMiddleName", typeof(string)); tbl.Columns.Add("MotherFirstName", typeof(string)); tbl.Columns.Add("MotherLastName", typeof(string)); tbl.Columns.Add("MotherMiddleName", typeof(string));
for (int j = 0; j < 100; j++)
{
DataRow
dr = tbl.NewRow();
for (int k = 0; k < 9; k++)
{
dr[k] = "TestNameToFill";
}
tbl.Rows.Add(dr);
}
ds.Tables.Add(tbl);
}
}
}
}
2. Rebuild the DataToSerialize.dll and create DataToSerialize.XMLSerializer.dll using SGEN utility.
3. Below two function in the console program Serialize the objet using XML Serializer and SGEN XML Serializer.
//Below function
serializes using sgen utility
static string SerializebySGEN()
{
Person
p = new Person();
p.Age = 29;
p.Name = "Satya
Narayan Sahoo";
StringBuilder
buildr = new StringBuilder();
StringWriter
writr = new System.IO.StringWriter(buildr);
PersonSerializer
mySerialzer = new PersonSerializer();
mySerialzer.Serialize(writr, p);
string
str = writr.ToString();
return
str;
}
//Below function
serializes using normal XML Serialization
static string NormalXMLSerialize()
{
Person
p = new Person();
p.Age = 29;
p.Name = "Satya
Narayan Sahoo";
StringBuilder
buildr = new StringBuilder();
StringWriter
writr = new System.IO.StringWriter(buildr);
XmlSerializer
mySerialzer = new XmlSerializer(typeof(Person)); mySerialzer.Serialize(writr, p);
string
str = writr.ToString();
return
str;
}
3. In the main method the above two functions are getting called and time taken to execute each function is getting printed on the console.
As seen in the result SGEN Serializer takes 31 millisecond while the XML Serializer takes 62 millisecond.

I hope this article will be useful.