Daniel Cazzulino's Blog : Boost XmlSerializer usability and performance by using strongly-typed serializers

Boost XmlSerializer usability and performance by using strongly-typed serializers

The XmlSerializer has a farily well-known problem: the first time you use it, it will generate a class to read instances of the received type, compile it, and then load it. Of course, this can take considerable time, directly related to the complexity of the type and all its members and member types.

Unlike common wisdom, this is something that can be avoided and I have explained (albeit rather confusingly) in a previous post. The usage was not great, though, as it was a command-line tool: ouch!

So, the Mvp.Xml project debuts with a new package: Mvp.Xml.Design. Now, in order to get a design-time version of a custom XmlSerializer for a type, you just assign the Mvp.Xml.XGen (renamed from the old SGen name that could be confused with a Whidbey tool of the same name), and you'll get a class to perform serialization of the type. The custom tool will take the first type in the file if multiple classes exist in it. Here's how it will look like:

A screenshot from VS using the tool

The package comes in two flavors: an MSI installer or the raw source. The former also installs the sources besides the custom tool.

Go download it!

posted on Thursday, October 21, 2004 8:03 PM by kzu

# re: Boost XmlSerializer usability and performance by using strongly-typed serializers @ Monday, November 14, 2005 2:16 AM

Does your tool work with .NET 1.1 (and Visual Studio .NET 2003). I've downloaded the install file that asks me for .NET 2.0.
Thanks a lot.

Andrea

# re: Boost XmlSerializer usability and performance by using strongly-typed serializers @ Monday, November 14, 2005 3:34 PM

I can't make this work, can you help me?

Sergio

# re: Boost XmlSerializer usability and performance by using strongly-typed serializers @ Monday, November 28, 2005 5:20 PM

The problem is because a few changes in c# code generated by .Net framework (after apply a fix, I guess). Is necessary to add the following lines to XmlSerializerGenerator class:

output = output.Replace("XmlSerializationWriter" + type.Name, "Writer");
output = output.Replace("XmlSerializationReader" + type.Name, "Reader");
output = output.Replace("public sealed class "+ type.Name + "Serializer", "public sealed class "+ type.Name + "SerializerBase");

// Give generated classes more friendly names.
output = output.Replace("public class XmlSerializationWriter" + type.Name,
@"/// <remarks/>
public class Writer");
output = output.Replace("public class XmlSerializationReader" + type.Name,
@"/// <remarks/>
public class Reader");

Ariel

# Improving the performance of the System.XmlSerializer in Framework 2.0 @ Thursday, December 01, 2005 2:38 PM

Today I was looking at the tools that are available in the new framework 2.0. I saw a tool called Sgen.exe...

Anonymous

# re: Boost XmlSerializer usability and performance by using strongly-typed serializers @ Thursday, December 01, 2005 2:44 PM

Dotnet has a tool to that is called sgen to accomplish the same thing. Check out http://www.dotnetjunkies.com/WebLog/drivenbydotnet/archive/2005/12/01/134105.aspx

geoffrey Samper

# re: Boost XmlSerializer usability and performance by using strongly-typed serializers @ Friday, December 02, 2005 12:12 PM

I've downloaded version 0.3 (for FW 1.1) but it doesn't work even on the simplest examples with the following stack trace:

#error Couldn't generate code.
/*
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentOutOfRangeException: StartIndex cannot be less than zero.
Parameter name: startIndex
at System.String.Substring(Int32 startIndex, Int32 length)
at Mvp.Xml.Design.CustomTools.XGen.XmlSerializerGenerator.GenerateCode(Type type, String targetNamespace)
at Mvp.Xml.Design.CustomTools.XGen.XGenRunner..ctor(String outputFile, String forType, String targetNamespace)
--- End of inner exception stack trace ---
...

Ariel, does your code fix this problem? If yes, where exactly it should be put?

Thanks a lot!

Andrew

# re: Boost XmlSerializer usability and performance by using strongly-typed serializers @ Wednesday, December 14, 2005 1:37 PM

Goeffrey: the sgen tool does not generate the event-rising class that my generator creates. My custom serializer (granted, it's based on the same code generated by the sgen tool) allows you to code to the incoming object in a streaming way, kind of SAX but strongly typed using objects.

kzu