Click here to Skip to main content
15,861,172 members
Articles / Programming Languages / XSLT

XSL Transform Code Generator for Visual Studio .NET

Rate me:
Please Sign up or sign in to vote.
4.88/5 (14 votes)
13 Feb 2021CPOL3 min read 144.4K   2.4K   75   31
A custom tool for Visual Studio .NET which runs an XSL transformation to generate code

Sample Image - TransformCodeGenerator.gif

Introduction

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... :-)

Conclusion

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.

License

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
Systems builder and developer, DotNet C#, Ruby, Golang, Linux, Cloud, Open Source contributor.

Comments and Discussions

 
QuestionVS2008, VS2010, VS2012, etc. Pin
Qwertie27-Oct-13 11:29
Qwertie27-Oct-13 11:29 
GeneralXSLT 2.0 Pin
Dmitri Nеstеruk1-Nov-07 20:10
Dmitri Nеstеruk1-Nov-07 20:10 
GeneralRe: XSLT 2.0 Pin
Chris Stefano1-Nov-07 20:18
Chris Stefano1-Nov-07 20:18 
GeneralRe: XSLT 2.0 Pin
Dmitri Nеstеruk3-Nov-07 22:34
Dmitri Nеstеruk3-Nov-07 22:34 
GeneralRe: XSLT 2.0 Pin
Chris Stefano4-Nov-07 7:10
Chris Stefano4-Nov-07 7:10 
GeneralSaxon.NET Pin
Dmitri Nеstеruk4-Nov-07 22:46
Dmitri Nеstеruk4-Nov-07 22:46 
GeneralRe: Saxon.NET Pin
Chris Stefano5-Nov-07 4:06
Chris Stefano5-Nov-07 4:06 
GeneralRe: Saxon.NET Pin
Dmitri Nеstеruk17-Nov-07 23:21
Dmitri Nеstеruk17-Nov-07 23:21 
GeneralRe: Saxon.NET Pin
Chris Stefano18-Nov-07 6:33
Chris Stefano18-Nov-07 6:33 
QuestionHow to create multiple output file for a sigle input file? Pin
Jonas A21-Sep-07 1:23
Jonas A21-Sep-07 1:23 
AnswerRe: How to create multiple output file for a single input file? Pin
Chris Stefano21-Sep-07 1:35
Chris Stefano21-Sep-07 1:35 
GeneralRe: How to create multiple output file for a single input file? Pin
Jonas A21-Sep-07 1:50
Jonas A21-Sep-07 1:50 
GeneralRe: How to create multiple output file for a single input file? Pin
Chris Stefano21-Sep-07 1:55
Chris Stefano21-Sep-07 1:55 
GeneralRe: How to create multiple output file for a single input file? Pin
Dmitri Nеstеruk4-Nov-07 22:48
Dmitri Nеstеruk4-Nov-07 22:48 
GeneralRe: How to create multiple output file for a single input file? Pin
Chris Stefano5-Nov-07 3:58
Chris Stefano5-Nov-07 3:58 
GeneralRe: How to create multiple output file for a single input file? Pin
Dmitri Nеstеruk5-Nov-07 6:44
Dmitri Nеstеruk5-Nov-07 6:44 
GeneralWinform to class Pin
cmschick30-Jan-07 1:58
cmschick30-Jan-07 1:58 
GeneralRe: Winform to class Pin
Chris Stefano30-Jan-07 4:10
Chris Stefano30-Jan-07 4:10 
GeneralNice! Pin
rstaylor14-Feb-06 18:20
rstaylor14-Feb-06 18:20 
GeneralXML, XSLT and Dynamic Business Rules Pin
Sluggish13-Feb-06 7:41
Sluggish13-Feb-06 7:41 
GeneralRe: XML, XSLT and Dynamic Business Rules Pin
Chris Stefano13-Feb-06 9:45
Chris Stefano13-Feb-06 9:45 
GeneralCode Generation Pin
ByteGhost12-Feb-06 23:38
ByteGhost12-Feb-06 23:38 
GeneralRe: Code Generation Pin
Chris Stefano13-Feb-06 3:27
Chris Stefano13-Feb-06 3:27 
QuestionVS2003 Support Pin
Chris Stefano10-Feb-06 4:16
Chris Stefano10-Feb-06 4:16 
GeneralUsing xml and codedom Pin
Marc Clifton8-Feb-06 17:16
mvaMarc Clifton8-Feb-06 17:16 
An interesting approach, using XSLT. I'll have to dig into this a bit more. Contrast with http://www.codeproject.com/csharp/xmlcompiler.asp[^], which uses xml and codedom.

Marc

Pensieve

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.