Code Generation in Visual Studio From XML Files - A Simpler Approach

In this post, we'll explore how to generate code from a simple XML model, with in Visual Studio - For a lot of scenarios

Code generation should be simple, and I wanted to do it by using a simple XML file as my model from with in Visual Studio. So, here is a quick wrapper I’ve created for Xml driven code generation for minimalists/minimal scenarios.

Introducing Super Simple XML driven code generation

Here we go – this is based on my ElasticObject implementation to generate code using Text Templates (TT/T4), by using simple XML files as a model. It is so simple that it won’t support the entire XML schema ;) - But I’ll live with that for now. So, the steps are simple, thanks to Nuget.

Step 1 - Get the ElasticObject Nuget Package

Or you can install this via the Nuget Package manager, it is your choice.

Step 2 – Enjoy

This should add the reference to ElasticObject library, and will add an Xml model file and a T4 template in the Solution Explorer (See the image). The xml file can be your model, and the TT file has a bit of code that wraps the model for generating what ever you need (source code, views, scaffolding etc). You can modify the xml and the TT file. Also, there is a ReadMe-ElasticObject.cs.txt which you can re-name to a CS file and view/run the tests to see the usage of ElasticObject.

What is in the Xml model file and TT generator?

The example model file contains some sample xml, it can be anything (infact, a reasonable subset of xml ;))

So, as you can see, the model can be anything, with in a valid root element. The example just mimics few classes and properties in that. Now, let us have a quick look at the example TT file added by the Nuget package. Don't get afraid, it is some simple T4 syntax - Refresh your T4 Skills from here, Oleg has everything for you to start with the T4 syntax (and much more).

Did I tell you you can use Visual T4 Editor Free edition to reduce the pain when you work with T4 Templates? Get it today from the VS Extensions gallery, otherwise you’ll end up sending hate mails to the guys who created the Visual Studio T4 editor.

In the below code, you could see that we are creating an elastic object from the xml, and then iterating the elements for generating the code – In case you are not familiar with ElasticObject, see my post here – It is just a wrapper dynamic object I created for loosely wrapping stuff like xml. Also, you could see this LIDNUG presentation where Shey is demoing the ElasticObject in between his dynamic talk.

Note that this requires C# dynamic support, and hence targets the .NET 4.0 runtime – So if you want to have code generation in other platforms with out dynamic support, add a .NET 4.0 project, keep your models and TT/T4s there, and add/link the generated files to other projects. This example just shows C# code generation from the model, but you can imagine what all things you can do ;)

A More Productive Example - CanML

In this example, we’ll explore how to create a simple Xml based markup language for defining shapes for HTML5 Canvas. We’ll achieve this by generating JavaScript code from the Xml definition, and this post is intended to be a pointer towards how you can leverage scaffolding and meta programming to simplify a lot of scenarios. If you don’t have a lot of kids to look after, modify this to create your own version of full fledged CanML (take that name) and let me know

What we are going to achieve?

You’ll be able to declare your canvas shapes using XML, for example see how I’m declaring a Heart shape and a Triangle slice. In the heart shape, I’m using absolute co-ordinates, and I’m using variables for triangle slice so that you can draw it anywhere.

And this will be rendered to an HTML5 Canvas as below. This trick is, we are generating the required JavaScript code from the above markup, using the technique I explained above

What about the Generated JavaScript?

If you are interested to see the JavaScript getting generated from the above Markup, see that below. If you examine the generated code closely along with the xml model we’ve above, you can see that we are following a couple of simple conventions to generate the code from the above markup.

Each <shape/> definition is wrapped as a method, with a canvas parameter (along with other parameters if param element is present)

The type attribute of the Shape

We are having some short cut notations (like to –> moveTo and bcurve-> bezierCurveTo)

If an element has a param attribute, it’ll be rendered as a method (example is moveTo)

If an element has a value attribute, it’ll be rendered as a property (example is

Note that all methods are generated with a canvas input parameter. And now, you may call these methods by linking to the generated CanvasShapes.js script file in your html page – See my index.cshtml ASP.NET MVC razor view where I invoke the allShapes method. Just get your actual canvas element, and pass it to draw, as shown below.

Tell me about the Actual Generation of JS from XML?

The actual generation is super simple – In fact this was just intended to be an sample use case for using the Elastic Object Nuget package for code generation.

To try this out manually,

Create a new ASP.NET MVC Project in Visual Studio.

Install the AmazedSaint.ElasticObject package as explained in my previous post – This will also add an Xml file and a TT (Text template file) to your project

Rename the Xml file to CanvasModel.xml – and copy the above XML code to the same

Rename the TT file CanvasShapes.tt, and add the below T4 code to the TT File

Here is the simple T4 code we are using for generating the JS Code. Oops, the actual conversion code is not even 25 lines, but this is enough to convey the idea, right? You can beef this up further, by adding some support for gradiants, transitions etc to make this full fledged. Keep the MDN Canvas documentation handy.