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.
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:
- I would develop a draft version of xml schema.
- I would create a simple console project in which I would start writing the class to be serialized.
- 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.
- 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
- 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.
- Press F5. The whole list of files will be compiled.
- 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.
- After successful compilation, select the required serializable type in the combo box; the Xml representation is displayed in the Serialized Xml text box.
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
I used to paint portraits in oil when I was kid.
For different reasons, I had not been able to maintain them as a result of which they have aged a lot :(.
The last time I went home, I took close up snapshots of my works with my Canon PowerShot A510, and now I am repairing them with Photoshop :).
Check out the initial stages of the “repair” (extracted the subjects and set the background to 0,0,0) below:
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”.
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.
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 )
public void ReadXml(System.Xml.XmlReader reader)
// Used while Deserialization
// Move past container
// Deserialize and add the BizEntitiy objects
while( reader.NodeType != XmlNodeType.EndElement )
entity = Serializer.Deserialize(reader) as BizEntity;
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.
Now that http://msmvps.com runs on CS (thanks a lot to Susan and team!), I will be cross posting for a while – here and at http://rakeshrajan.com/blog/.
Also, since some of the readers complain that the LuxLegend skin I am using isn’t really attractive (maybe because it’s pretty dark), I have set the skin here to LuxLegend – light
Friends, now that I have bought my own little niche in the web world, I have decided to shift this blog to this cool new place. It runs on Community Server with LuxLegend as the skin. Please update your links.
For your convenience, I have duplicated these posts at the new blog. However note that I haven’t copied comments or trackbacks. Please refer to this for the same.
Wish you a great time out there!
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.
This is something that comes up frequently.
Check out the below link I wrote a long long time ago. Hope it has some helpful information. Please drop me a mail if you have questions.