Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Transform XML via XSLT-Stylesheets

0.00/5 (No votes)
9 Nov 2004 1  
Shows how you can make a transformation of XML pages via XSL transform stylesheets, under C#.

Introduction

With XML you can describe data hierarchically without caring about the later output format, and with XSL-Transformations you have the ability to convert the content of that XML document into various formats such as text, xml, html, ... if necessary you can replace, add, reformat the content, ... using the xsl-functionality like 'for-each', 'if/then/else', 'select' ...

For the proper transformation you have to build such an XSL-Stylesheet and you could include it into the XML document as well as you could use the .NET - Framework to do this transformation, e.g. for using it to create Websites using ASP.NET oder to convert one dataformat into another using the intermediate step XML, ... many other applications are imaginable.

How dows it works

You have to create an XPathDocument (which contains the XMLDocument) and an XSLTransform (the XSLStylesheet) and to apply the transformation to the XMLDocument, what anger me was that the only input resp. output to this transformation was via streams so first i made it via temporary files, but in productive use this is futile, so I browsed the helpfiles and finally the code below came out, bit by bit I understand slowly the concept behind the streams and readers/writers in (C#).NET so I wanted to share this insight...

 

using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;

namespace XMLTransformer
{
  public class Transformer
  {
    public Transformer()
    {}

     /// <summary>
     /// transforms XML via XSLTransformation
     /// </summary>
     /// <param name="XMLPage"></param>
     /// <param name="XSLStylesheet"></param>
     /// <returns>The transformed Page</returns>
     public static string Trafo(string XMLPage, string XSLStylesheet, out string ErrMsg)
     {
         //the outputs 
         string result="";
         ErrMsg="";

         try
         {
             //read XML
            TextReader tr1=new StringReader(XMLPage);
            XmlTextReader tr11=new XmlTextReader(tr1);
            XPathDocument xPathDocument=new XPathDocument(tr11);
    
            //read XSLT
            TextReader tr2=new StringReader(XSLStylesheet);
            XmlTextReader tr22=new XmlTextReader(tr2);
            XslTransform xslt = new XslTransform ();
            xslt.Load(tr22);
    
            //create the output stream
            StringBuilder sb=new StringBuilder();
            TextWriter tw=new StringWriter(sb);

            //xsl.Transform (doc, null, Console.Out);
            xslt.Transform(xPathDocument,null,tw);
    
            //get result
            result=sb.ToString();
         }
         catch (Exception ex)
         {
            //Console.WriteLine (ex.Message);
            ErrMsg=ex.Message;
         }
         return result;
      }//Trafo
   }//class
}//namespace

I think the Code is self-explanatory, for further informations about XML, XSLT and XPATH visit http://www.w3schools.com/

 

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