Visual Studio XGen - Example

If you are an XML and XSLT guru then this should be home turf for you. If you are new to XML and XSLT then you might want to read up a little on XML and XSLT. But for both the expert and the novice, here's a simple example.

The xtransform document

First you need an xtransform document. The xtransform document should have extension .xtr.xml. It describes how to use the .xslt document to convert the .xml document into the output document. You can also give an arbitrary number of parameters.
<?xml version="1.0" encoding="utf-8" ?>
<xtransformations xmlns = "">
  <xtransform xsource="input.xml" xtemplate="template.xslt" output="output.cs">
    <param name="type" value="DAL"/>
  <xtransform xsource="input.xml" xtemplate="[xgenlib]\template.xslt" output="output.sql">
    <param name="type" value="SP"/>
Please note the use of as a name space. Also, note the use of [xgenlib]. That is a project variable. The use of [xgenlib] assumes that there is a project called xgenlib (or XGenLib, it is case insensitive) and the angle brackets and everything between them will be replaced with the working folder for that project.

The .xml document

<?xml version="1.0" encoding="utf-8" ?>
  using System.Xml;
  using System;
  using System.Runtime.Serialization;
  namespace ConsoleApplication1 {
    <exception name="ExampleException1" base="ApplicationException" />
    <exception name="ExampleException2" base="ApplicationException" />

Please note that you can write normal code (C# in this example) in the xsource document. In this case the "namespace ConsoleApplication1 {" will be moved verbatim to the result file. You need to remember though that the less than sign has special meaning. So if you want to write "x < 2" then you need to write &lt; instead of the less than sign.

The .xslt file

We also need the template referred to in the xtemplate attribute. Please note how the "type" parameter is declared and used.

<?xml version="1.0" encoding="UTF-8" ?>
<stylesheet version="1.0" xmlns="">
<param name="type"/>
<output method="text" />

<template match="xsource">
  //<value-of select="$type"/>
  <apply-templates />

<template match="exception">
  public class <value-of select="@name" /> : <value-of select="@base" />
    // Default constructor
    public <value-of select="@name" />() : base()
    // Constructor with exception message
    public <value-of select="@name" />(string message) : base(message)
    } // Constructor with message and inner exception
    public <value-of select="@name" />(string message, Exception inner)
      : base(message,inner)
    // Protected constructor to de-serialize data
    protected <value-of select="@name" />(SerializationInfo info, 
    StreamingContext context) : base(info, context)

So there you have it.