Click here to Skip to main content
13,554,429 members
Click here to Skip to main content
Add your own
alternative version


70 bookmarked
Posted 8 Feb 2006

XSL Transform Code Generator for Visual Studio .NET

, 8 Feb 2006
Rate this:
Please Sign up or sign in to vote.
A custom tool for Visual Studio .NET which runs an XSL transformation to generate code.

Sample Image - TransformCodeGenerator.gif


After reading an article on CodeProject entitled VS.NET CodeDOM-Based Custom Tool for String Resource Management by Jasmin Muharemovic, and finding the StringResourceTool custom tool for string resources, I discovered how easy it is to create a custom tool for Visual Studio .NET.

The Problem

In order to generate code, you use the CodeDOM objects, which appear to be fairly complex and cumbersome to code. Also, if the "source" file for the code generation is in some text format, then a whole lot of string parsing needs to be performed to extract the "parameters" required to generate the appropriate code.

Another issue is that, each time you want to generate something else, you have to create another custom tool and register it in Visual Studio. This could lead to a "DLL hell" of custom tools, as well as trying to remember each one's name would be difficult.

The Solution: XML + XSLT

XML takes all this hard string parsing stuff away, and provides a powerful mechanism (XPath) for extracting whatever you want out of the XML file. XSLT provides a flexible mechanism for producing output in any format. Thus, I adapted the custom tool code to run an XSL transform instead of producing the generated code.

In this way, each time I want to create a new type of code generator, I just invent a new XML schema for the source file and a corresponding XSL transform to generate the required code.

How it Works

There are two requirements for the TransformCodeGenerator to work properly:

  • The XML source files root element must contain an attribute called "transformer". The value of this attribute specifies which XSLT file to use for the transformation.
  • The XSLT filename may be fully qualified, or just the file name, in which case, the file must reside in the same directory as the XML file (in the project), or in the same directory as the TransformCodeGenerate.dll files.

Add your XML file to your Visual Studio solution, and set the CustomTool property to TransformCodeGenerator. Each time you modify and save the XML file, the custom tool will regenerate your code.

Sample Project

I created two sample XML files to illustrate the possible usage for this custom tool.

I invented an XML schema which describes a WinForms form with some controls on it. Check out the "MyXMLForm.xml" file in the test project. The accompanying XSLT transform, "MakeXMLFormCode.xslt", transforms this into code which creates a class which has fields for each control, creates a new instance of each control, sets the control properties, and adds them to the form.

The second example is for "type-safe" DataTables. This XML file describes a database table in a more friendly way than an XSD file. This time, the XSLT generates two classes which inherit from DataTable and DataRow, respectively. The DataRow class will have type-safe properties for each column that the DataTable contains. You could take this sample further by creating a tool which reads a database schema and produces this XML file... :-)


XML + XSLT provides a powerful way to produce code generated code, and now with the TransformCodeGenerator custom tool for Visual Studio, it's a whole lot easier than running an external tool to run the transform.


Please note: This article does not intend to go into the details of how to author XML files or XSLT transformations.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Chris Stefano
Software Developer (Senior)
South Africa South Africa
I'm a developer specializing in software for the financial services industry particularly in the decision support area. Currently my specialty is in investment compliance.

You may also be interested in...


Comments and Discussions

QuestionVS2008, VS2010, VS2012, etc. Pin
Qwertie27-Oct-13 11:29
memberQwertie27-Oct-13 11:29 
GeneralXSLT 2.0 Pin
Dmitri Nesteruk1-Nov-07 20:10
memberDmitri Nesteruk1-Nov-07 20:10 
GeneralRe: XSLT 2.0 Pin
Chris Stefano1-Nov-07 20:18
memberChris Stefano1-Nov-07 20:18 
GeneralRe: XSLT 2.0 Pin
Dmitri Nesteruk3-Nov-07 22:34
memberDmitri Nesteruk3-Nov-07 22:34 
GeneralRe: XSLT 2.0 Pin
Chris Stefano4-Nov-07 7:10
memberChris Stefano4-Nov-07 7:10 
GeneralSaxon.NET Pin
Dmitri Nesteruk4-Nov-07 22:46
memberDmitri Nesteruk4-Nov-07 22:46 
GeneralRe: Saxon.NET Pin
Chris Stefano5-Nov-07 4:06
memberChris Stefano5-Nov-07 4:06 
GeneralRe: Saxon.NET Pin
Dmitri Nesteruk17-Nov-07 23:21
memberDmitri Nesteruk17-Nov-07 23:21 
GeneralRe: Saxon.NET Pin
Chris Stefano18-Nov-07 6:33
memberChris Stefano18-Nov-07 6:33 
QuestionHow to create multiple output file for a sigle input file? Pin
Jonas A21-Sep-07 1:23
memberJonas A21-Sep-07 1:23 
AnswerRe: How to create multiple output file for a single input file? Pin
Chris Stefano21-Sep-07 1:35
memberChris Stefano21-Sep-07 1:35 
GeneralRe: How to create multiple output file for a single input file? Pin
Jonas A21-Sep-07 1:50
memberJonas A21-Sep-07 1:50 
GeneralRe: How to create multiple output file for a single input file? Pin
Chris Stefano21-Sep-07 1:55
memberChris Stefano21-Sep-07 1:55 
GeneralRe: How to create multiple output file for a single input file? Pin
Dmitri Nesteruk4-Nov-07 22:48
memberDmitri Nesteruk4-Nov-07 22:48 
GeneralRe: How to create multiple output file for a single input file? Pin
Chris Stefano5-Nov-07 3:58
memberChris Stefano5-Nov-07 3:58 
GeneralRe: How to create multiple output file for a single input file? Pin
Dmitri Nesteruk5-Nov-07 6:44
memberDmitri Nesteruk5-Nov-07 6:44 
GeneralWinform to class Pin
cmschick30-Jan-07 1:58
membercmschick30-Jan-07 1:58 
GeneralRe: Winform to class Pin
Chris Stefano30-Jan-07 4:10
memberChris Stefano30-Jan-07 4:10 
GeneralNice! Pin
rstaylor14-Feb-06 18:20
memberrstaylor14-Feb-06 18:20 
GeneralXML, XSLT and Dynamic Business Rules Pin
Sluggish13-Feb-06 7:41
memberSluggish13-Feb-06 7:41 
GeneralRe: XML, XSLT and Dynamic Business Rules Pin
Chris Stefano13-Feb-06 9:45
memberChris Stefano13-Feb-06 9:45 
GeneralCode Generation Pin
ByteGhost12-Feb-06 23:38
memberByteGhost12-Feb-06 23:38 
GeneralRe: Code Generation Pin
Chris Stefano13-Feb-06 3:27
memberChris Stefano13-Feb-06 3:27 
QuestionVS2003 Support Pin
Chris Stefano10-Feb-06 4:16
memberChris Stefano10-Feb-06 4:16 
GeneralUsing xml and codedom Pin
Marc Clifton8-Feb-06 17:16
protectorMarc Clifton8-Feb-06 17:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04-2016 | 2.8.180515.1 | Last Updated 8 Feb 2006
Article Copyright 2006 by Chris Stefano
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid