XML transformation using Xslt in C#
http://www.csharpfriends.com
World's Greatest C# Community    
Home Articles C# Forums Books C# Syntax C# Spec C# Jobs free Source Code Advertise About
 
Control Panel

[ Sign In / register ]
Points   
Notes 
My Forums
My Tutorials
My Profile

Resources

Learn
 Articles
 QuickStarts
 C# Spec
 Whitepapers
 Tools
 Class Browser
 C# Code Generator
 Links
 Misc Rss Feeds
 Code Highlight
 411 Directory
 FREE magazines

Reviews
  ASP.NET Hosting

Source Code
 Get Version 1.0



C# Consulting
AspDotNetStoreFront
Chapter:   XML
Current Lesson:
XML transformation using Xslt in C#
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Remoting in C# [next Lesson]  Extending XSLT using C#
XML transformation using Xslt in C#
  by: maheshcr

Xml transformation using Xslt in C#

by: maheshcr

Summary:

Xml is a meta-markup language that provides a format for describing data. Xslt is one way to consume and transform this data into a variety different formats, including Xml, html and wml. In this article we shall learn how to transform a simple Xml document using Xslt.

The .NET framework provides rich support for the manipulation of Xml documents.

The main namespaces that provide us with the needed classes are:

System.Xml
System.Xml.XPath
System.Xml.Xsl


Xml Transformation steps

The steps to transform an Xml document are

1) Load the Xml document
XPathDocument myXPathDoc = new XPathDocument(<xml file path>) ;
2) Load the Xsl file
XslTransform myXslTrans = new XslTransform() ;
myXslTrans.Load(<xsl file path>);
3) Create a stream for output
XmlTextWriter myWriter = new XmlTextWriter("result.html",null) ;
4) Perform the actual transformation
myXslTrans.Transform(myXPathDoc,null,myWriter) ;
Sample Code

Check the code below for a C# command line application that transforms Xml with Xsl.

Compile and run the application. Usage is as follows:
XmlTransformUtil.exe <xml file path> <xsl file path>
You could use the provided sampledoc.xml and sample.xsl to have a go at it. You should see a 'result.html' in the same folder as the application with the results of the transformation.
using System ;
using System.IO ;
using System.Xml ;
using System.Xml.Xsl ;
using System.Xml.XPath ;

public class XmlTransformUtil{

    public static void Main(string[] args){
        
        if (args.Length == 2){
            
            Transform(args[0], args[1]) ;
            
            
        }else{
            
            PrintUsage() ;
            
        }
        
    
    }
    
    public static void Transform(string sXmlPath, string sXslPath){
        
        try{
            
            //load the Xml doc
            XPathDocument myXPathDoc = new XPathDocument(sXmlPath) ;

            XslTransform myXslTrans = new XslTransform() ;
            
            //load the Xsl 
            myXslTrans.Load(sXslPath) ;
            
            //create the output stream
            XmlTextWriter myWriter = new XmlTextWriter
				("result.html", null);
            
            //do the actual transform of Xml
            myXslTrans.Transform(myXPathDoc,null, myWriter);        

            myWriter.Close() ;


        }catch(Exception e){

            Console.WriteLine("Exception: {0}", e.ToString());
        }
        
    }
    
    
    public static void PrintUsage(){
        Console.WriteLine
		("Usage: XmlTransformUtil.exe <xml path> <xsl path>");
    }

}
sampledoc.xml (control-a to copy)



sample.xsl (control-a to copy)


1 

Chapter:  XML
Current Lesson:
XML transformation using Xslt in C#
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Remoting in C# [next Lesson]  Extending XSLT using C#


Today's Top Movers
vulpes 6800
MadHatter 2220
jal 867
Jeff1203 857
muster 791

Yesterday Top Movers
shakti sin.. 9
MadHatter 3
Al_Pennywo.. 2
C#fanatic 2
simboy 1

Monthly Leaders
vulpes 6800
MadHatter 2260
jal 867
Jeff1203 857
muster 791

Top Members
mosessaur 18457
Rincewind 7074
stanleytan 6995
vulpes 6800
Gsuttie 6046

Top of Page

Advertise | About | Link To Us | Privacy Notice Copyright © 2003 - 2005 CSharpFriends.com  All Rights Reserved  Visual C# Developer Center