Simple C# XSL Transformation

“XSLTransform.cs”

                                    

And here is a simple C# translator that takes an XML file sends it through an XSL file transform and saves the output.  It is intended to be used as a console application.  This can be easily used to apply the CSV transform XSL to Vocabulary.xml.

 

 

// Created by Noah Coad, coad.net/noah, noah@coad.net, 4/10/04

 

using System;

using System.IO;

using System.Xml;

using System.Xml.Xsl;

using System.Diagnostics;

 

namespace XSLTransform

{

   class Transform

   {

      private Transform(string[] args)

      {

         // Make sure at least three command

         // line arguments are specified

         if (args.Length < 3) {ShowSyntax(); return;}

 

         XslTransform xsl = new XslTransform();

 

         // Attempt to load the XSL

         try {xsl.Load(args[1]);}

         catch (XmlException e) {Console.WriteLine(“Could not load XSL transform: ” + args[1] + “\n\n” + e.Message); return;}

         catch (XsltException e) {Console.WriteLine(“Could not process the XSL: ” + args[1] + “\n\n” + e.Message + “\nOn line ” + e.LineNumber + ” @ ” + e.LinePosition); return;}

 

         // Attempt to transform the XML

         try {xsl.Transform(args[0], args[2]);}

         catch (XmlException e) {Console.WriteLine(“Could not perform transform on: ” + “\n\n” + e.Message); return;}

      }

 

      [STAThread]

      static void Main(string[] args)

      { new Transform(args); }

 

      private void ShowSyntax()

      { Console.Write(“XSLTransform Xml-URL Xsl-URL Output.txt”); }

   }

}

 

 

 

6 thoughts on “Simple C# XSL Transformation”

  1. Cool, I appreciate the simplicity of this very much : )

    My Visual Studio 2003 claims that this method is obsolete:

    xsl.Transform(xmlFileName, outputFileName);

    …Any ideas how to comply with the suggested change?

  2. Its like you read my thoughts! You seem to grasp a lot approximately this,
    such as you wrote the ebook in it or something. I think that you
    can do with a few percent to drive the message home a bit, however instead
    of that, that is fantastic blog. An excellent read. I will
    certainly be back. Check out my website to get more info about bodybuilding, if you like.

  3. I do agree with all the concepts you have presented for your post.

    They’re really convincing and will certainly work. Still, the posts are too brief for starters. Could you please lengthen them a little from subsequent time? Thanks for the post. Check out my website to get more info about forex, if you like.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>