Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » XML .NET » Transforming Visual Studio .NET Projects into NANT files using XSLT and .NET

Transforming Visual Studio .NET Projects into NANT files using XSLT and .NET

I was looking for a solution on how to execute an XSL transform on a C# project file and convert it to a Nant build file. Nant provides the xsl file to do the conversion under the open source license agreement, but Nant does not provide away of producing the transformation.

Author Rank:
Total page views :  85883
Total downloads :  906
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
XSLTCodeMG.zip
 
Become a Sponsor

I was looking for a solution on how to execute an XSL transform on a C# project file and convert it to a Nant build file. Nant provides the xsl file to do the conversion under the open source license agreement ( courtesy of Gordon Weakliem), but Nant does not provide away of producing the transformation.In my search I found some expensive solutions and decided that I didn't want to spend the $100-$1000 dollars on a tool to do this since XSLT transformation is built right into the .NET framework.The tool took all of 15 minutes to write in .NET so I think it was worth the time spent.

Building with Nant

NAnt is an Open Source product that allows you to perform some powerful build, documentation and deployment solutions for .NET.  NAnt is like NMake except about 1000 times easier to use and easier to understand because it is written in XML.  NAnts files are divided into Projects, Properties, Targets and Tasks.  Tasks allow you to do many things such as compile C# code, file management, call other builds, or launch other applications.  NAnt works with other Open Source Solutions as well such as NDoc.  NDoc generates documentation from assemblies and Xml document files (Xml Document files are generated from the csc compile by setting the /doc option.  This /doc option reads the summary (triple-slash) comments out of the source code and generates an Xml file from it.)  NDoc can generate beautiful MSDN style chm help files from the code.  If you document all of your code properly, you will produce some amazing class documentation with this tool.  

NAnt also has a whole Nant-Contrib Open Source Project which creates auxiliary tasks to NAnt to do things like create MSI files for deployment.  It really is an extremely powerful tool and I encourage you to download and try it.  For performing production builds where you may need to accomplish things outside the capabilities of Visual Studio, it may prove to be a great solution.

Transforming through XSLT

XSLT (Xml Style Sheet Language Transform) allows you to create an active template that will act on an XML file and convert it to another XML file format.  XSLT uses XPath to match elements in the source document and applies template rules to alter the results in the destination document.  XPath is a sort of language that allows you to filter nodes inside an XML document.  Below is a sample XSL Transform that will convert a list of books and authors in an xml data file and convert the results to an HTML table of title and author.

Listing 1 - XSLT to transform to an HTML table 

<?xml version="1.0" encoding="ASCII"?>
<
xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform>"
<
xsl:template match="/">
<
html>
<
body>
<
h2>My Book Collection</h2>
<
table border="1">
<
tr bgcolor="#9ace34">
<
th align="left">Title</th>
<
th align="left">Author</th>
</
tr>
<
xsl:for-each select="catalog/library">
<
tr>
<
td><xsl:value-of select="title"/></td>
<
td><xsl:value-of select="author"/></td>
</
tr>
</
xsl:for-each>
</
table>
</
body>
</
html>
</
xsl:template>
</
xsl:stylesheet>

Listing 2 - XML Data To Transform 

<?xml version="1.0" encoding="ASCII"?>
<?
xml-stylesheet type="text/xsl" href="booklibrary.xsl"?>
<
catalog>
<
library>
<
title>Digital Fortress</title>
<
author>Dan Brown</author>
<
country>USA</country>
<
publisher>Columbia</publisher>
<
price>7.99</price>
<
year>2003</year>
</
library>
.
.
.
</catalog>

Transforming XML  in .NET

.NET provides us with the System.Xml.Xsl namespace to activate our Xsl Transforms.  At the heart of this namespace is the XslTransform class.  The XslTransform class makes transforming Xml a fairly simple task.Below (Listing 3) is the code from our XslTransformer that allows us to read an input file, perform an Xsl Transform on the file and produce an output file using the XslTransform class. The XPathDocument class is used to load the VS.NET project file which serves as our input into the Xsl Transform.  The outputted Nant build file is produced using the XmlTextWriter class.The transform is carried out using the Transform method of the XslTransform class.Note it only takes a few lines of code to perform this whole process using the powerful classes provided by .NET.

Listing 3 - Using the XslTransform class to transform the csproj file

// read the input file to be transformed
XPathDocument xpathdocument = new XPathDocument(browserProject.FileName);
// read the xslt transform to perform on the input file
XslTransform xslt = new XslTransform();
xslt.Load(browserXSLT.FileName);
// create a file for output.
XmlTextWriter writer = new XmlTextWriter(browserNantOutput.FileName, Encoding.Unicode);
writer.Formatting=Formatting.Indented;
// perform the transform on the input file and write out the result transformation
xslt.Transform(xpathdocument, null, writer, null);
writer.Close();
// display the output in the edit box
StreamReader sr = new StreamReader(browserNantOutput.FileName);
txtOutput.Text = sr.ReadToEnd();
sr.Close();

Persisting Data in the App.Config File

One thing I wanted to try to do in this project was to persist the fields in the dialog by writing them to the App.Config File.   Upon finding the System.Configuration.ConfigurationSettings.AppSettings class, I thought, Great!  .NET provides a nice convenient way of persisting data to the configuration file.  Then I ran into the snag of actually trying to use this class to persist data to the config file.  I found that the class reads the data fine out of the file.   The problem is that the class cannot write the data to the keys.  What is even more confusing is that a Set method is even provided in the AppSettings class, but the class doesn't actually allow you to use this method.  It was time to search and see if anyone else was having the same problem.  In my research and with a little coding, I ended up creating the following class in Listing 5 to solve the problem.  Basically the class ConfigHandler uses the convenient System.Xml namespace and XPath to read and write the keys and values in a configuration file.  We can use the XmlDocument class to Load the configuration file and then the XPath query allows us the select the desired node out of the XmlDocument. Once we have the XmlNode of interest we can either read or write to it.The XPathquery,
/configuration/appSettings/add[@key = <key> allows us to select a single node with the particular <key> out of the Xml Hierarchy configuration->appsettings->add.

Listing 4 -   App.Config file containing persistent form values 

<?xml version="1.0" encoding="utf-8"?>
<
configuration>
<
appSettings>
<!
-- User application and configured property settings go here.-->
<!
-- Example: <add key="settingName" value="settingValue"/> -->
<
add key="browserProject.FileName" value="C:\NAnts\tests\NAnt.Tests.csproj" />
<
add key="browserNantOutput.FileName" value="C:\temp0" />
<
add key="browserXSLT.FileName" value="C:\NAnts\vsconvert.xsl" />
</
appSettings>
</
configuration>

Listing 5 -  Class to handle persistence in the App.Config file 

public class ConfigHandler
{
public ConfigHandler()
{
}
/// <summary>
/// This is the configuration file path property. Set this before using this class
/// </summary>
static string _configurationPath = "App.Config";
static public string ConfigurationPath
{
get
{
return _configurationPath ;
}
set
{
_configurationPath =
value ;
}
}
/// <summary>
/// Replace the key and value pair in the configuration file
/// </summary>
/// <param name="key"></param>
/// <param name="val"></param>
static public void replaceConfigSettings(string key, string val)
{
XmlDocument xDoc =
new XmlDocument();
string sFileName = _configurationPath;
try
{
// load the configuration file
xDoc.Load(sFileName);
// find the node of interest containing the key using XPATH
XmlNode theNode = xDoc.SelectSingleNode(@"/configuration/appSettings/add[@key = '" + key + "\']");
// Set the new value for the node
if (theNode != null)
theNode.Attributes["value"].Value = val;
// lop off file prefix if it exists
if(sFileName.StartsWith(file:///))
sFileName = sFileName.Remove(0,8);
// save the new configuration settings
xDoc.Save(sFileName);
xDoc =
null;
}
catch(Exception ex)
{
#if DEBUG
System.Windows.Forms.MessageBox.Show("replaceConfigSettings()"+ex.Message);
#else
System.Windows.Forms.MessageBox.Show("Unable to save changes");
#endif
xDoc = null;
}
}
/// <summary>
/// Retrieve the configuration value given the key
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
static public string GetConfigSetting(string key)
{
XmlDocument xDoc =
new XmlDocument();
string sFileName = _configurationPath;
// make sure the file exists, if not return
if (File.Exists(sFileName) == false)
{
return "";
}
try
{
// load the configuration file
xDoc.Load(sFileName);
// find the node of interest from the key
XmlNode theNode = xDoc.SelectSingleNode(@"/configuration/appSettings/add[@key = '" + key + "\']");
// retrieve the nodes value if it exists
if (theNode != null)
return theNode.Attributes["value"].Value;
xDoc =
null;
}
catch(Exception ex)
{
#if DEBUG
System.Windows.Forms.MessageBox.Show("replaceConfigSettings()"+ex.Message);
#else
System.Windows.Forms.MessageBox.Show("Unable to load changes");
#endif
xDoc = null;
}
return "";
}
}

Conclusion

.NET  gives us a lot of power over XML technologies with the System.Xml, System.Xml.Xsl, and System.Xml.XPath namespaces.  With these namespaces and the classes contained within, we can manipulate Xml by running Xsl Transforms on files and performing XPath queries.  Because both Nant and VS.NET projects are XML files, we can utilize the power of Xsl to convert between the two.  

NAnt and NDoc are two very powerful Open Source tools that you can add to your .NET arsenal.  If you are going to begin using NAnt, I encourage you to look at the simple tutorial provided on sourceforge to begin your NAnt journey.  In the meantime, if you run into a situation where think you may not be able to do it with Visual Studio.NET,never say CAn't, when you may be able to do it with NAnt.


Login to add your contents and source code to this article
 About the author
 
Mike Gold
Michael Gold is President of Microgold Software Inc., makers of the WithClass UML Tool. His company is a Microsoft VBA Partner and Borland Partner. Mike is a Microsoft MVP and founding member of C# Corner. He has a BSEE and MEng EE from Cornell University and has consulted for Chase Manhattan Bank, JP Morgan, Merrill Lynch, and Charles Schwab. Currently he is a senior developer at Finisar Corp. He has been involved in several .NET book projects, and is currently working on a book for using .NET with embedded systems. He can be reached at mike@c-sharpcorner.com
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010 Professional
Microsoft Visual Studio 2010 Professional will launch on April 12, but you can beat the rush and secure your copy today by pre-ordering at the affordable estimated retail price of $549 (US). Pre-order now.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
XSLTCodeMG.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Click Here for 6 Months Free! Powerful ASP.NET Hosting at your Fingertips!
Become a Sponsor
 Comments
vsconvert.xsl is missing from the zip file by Mukremin On March 30, 2007
vsconvert.xsl is missing from the project folder
Reply | Email | Delete | Modify | 
Re: vsconvert.xsl is missing from the zip file by Mike On April 6, 2007
The XSLT file is part of nant-contrib
Reply | Email | Delete | Modify | 
Re: Re: vsconvert.xsl is missing from the zip file by Anil On March 13, 2008

Tool dont have buttons.

Is there new project?

Reply | Email | Delete | Modify | 
Visual Studio 2008 by Catherine On September 23, 2008
Is there a version for VS2008?
Reply | Email | Delete | Modify | 
Re: vsconvert.xsl is missing from the zip file by Angie On May 7, 2009
how to create vsconvert.xsl??
Reply | Email | Delete | Modify | 
how is this different from the slingshot task provided by Nant-contrib? by brian On September 4, 2009

I'm new to NAnt and was thankful for your article pointing me to Nant-contrib. I was just wondering if the slingshot task provided in nant-contrib acheives the same thing as what your article is trying to acheive.. produce XML based on the .NET solution file. I assume the task may've been released at a date later that when this article was posted.

Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.