Class to Xml tool (test version)

Summary


CTX (Class to Xml) tool is a very simple tool which provides you a code editing environment in C# and VB.NET, compiles the source codes and lets you see the Xml output of all serializable types once successfully compiled.



Intro


Do you use Xml Serialization in .NET – if yes, you might be interested in this tool!


Have you ever thought “Hmm…now that I have written a serializable class, I wonder how will it look when it is Xml Serialized”?


If yes, you are in good company :). Sometime back I was involved in a project in which I had to develop a lot of business entity classes which would be Xml Serialized. Since these classes had to adhere to a specific schema, I had a tough time synchronizing the Xml Attributes and the final xml schema it generated when xml serialized.


The sequence of how I would go about doing this was like this:

  1. I would develop a draft version of xml schema.
  2. I would create a simple console project in which I would start writing the class to be serialized.
  3. This project will have a small code snippet which will create an instance of the serializable class and xml serialize it and then finally print out the xml output.
  4. I will keep comparing this output with my xml schema until they are in sync.


As I got involved in more and more projects (many of them my own :) ) which came to have similar requirements, I decided to create a little tool which would automate the above tasks.


Introducing the Class to Xml (CTX) tool – a very simple tool which provides you a code editing environment in C# and VB.NET, compiles the source codes and lets you see the Xml output of all serializable types once successfully compiled.


How to use the tool



  1. Open up the source code files of the serializable classes you want to get the xml for (including all the files that are referenced) and select the language.
  2. Press F5. The whole list of files will be compiled.
  3. If compilation is successful, the Output pane would list all serializable types available in the assembly. If compilation failed, the Errors pane would list the errors.
  4. After successful compilation, select the required serializable type in the combo box; the Xml representation is displayed in the Serialized Xml text box.


Download


The current test version source code (probably terrible) in Visual Studio 2005 is available here (The site is down due to site maintenance, please wait while I resolve this ASAP).
Do check it out and let me know your comments.


I plan to finalize the code soon…er, I have no idea how “soon” though :)

XML Serialize IDictionary types (Hashtable, DictionaryBase etc.)

Problem


You want to serialize a type; however it:

  • implements IDictionary, or
  • derives from DictionaryBase, contains a member which implements IDictionary or derives from DictionaryBase,
which causes XmlSerializer to throw an exception:
“System.NotSupportedException: The type X is not supported because it implements IDictionary”.


Solution


Override XmlSerialization by making the type implement the System.Xml.Serialization.IXmlSerializable class. Define how you want the object to be serialized in XML in the WriteXml method, and define how you could recreate the object from an xml string in the ReadXml method.


Code


Download the full code here.


The WriteXml and ReadXml methods used in the sample follow.

public void WriteXml(System.Xml.XmlWriter writer)
{
	// Used while Serialization

	// Serialize each BizEntity this collection holds
	foreach( string key in this.Dictionary.Keys )
	{
		Serializer.Serialize(writer, this.Dictionary[key]);
	}
}

public void ReadXml(System.Xml.XmlReader reader)
{
	// Used while Deserialization

	// Move past container
	reader.Read();

	// Deserialize and add the BizEntitiy objects
	while( reader.NodeType != XmlNodeType.EndElement )
	{
		BizEntity entity;

		entity = Serializer.Deserialize(reader) as BizEntity;
		reader.MoveToContent();
		this.Dictionary.Add(entity.Key, entity);
	}
}

Description


By default, XML Serialization does not serialize types that implement IDictionary; it throws an exception when you attempt to do so. This comes quite in the way when you need to switch serialization techniques, or have custom collections which derive from DictionaryBase or any other sort of scenario.


The workaround for this problem is to let XML Serializer know how to serialize your IDictionary types yourself, by implementing the System.Xml.Serialization.IXmlSerializable class. This interface declares three methods – ReadXml, WriteXml and GetSchema.


When XmlSerializer attempts to serialize a type and finds that it implements IXmlSerializable, the serializer would simply pass control to the ReadXml (during deserialization) or WriteXml (during serialization) methods. In other words, you yourself handle how serialization and deserialization should happen.

Off to the Global Summit! :)

A few more hours, and I will be on board the first flight of my trip to Redmond for attending the Global MVP Summit.


It’s quite wonderful to know that within a few hours I will be meeting people I have only “seen” at newsgroups.


Hope the journey will go on fine.


Updates after I come back on October 5th.


See ya!