Click here to Skip to main content
Licence 
First Posted 9 Nov 2004
Views 62,217
Bookmarked 17 times

Transform XML via XSLT-Stylesheets

By | 9 Nov 2004 | Article
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

About the Author

Ekki

Software Developer

Germany Germany

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralEdit Please PinmemberRavi Sant22:20 12 May '11  
GeneralSome comments PinmemberJaime Olivares13:41 2 Sep '09  
GeneralThanks Pinmemberam.piazza3:58 5 Oct '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.5.120517.1 | Last Updated 10 Nov 2004
Article Copyright 2004 by Ekki
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid