How to apply an XSL transformation to an XML document by using Visual C#

Article translations Article translations
Article ID: 307322 - View products that this article applies to.
This article was previously published under Q307322
For a Microsoft Visual Basic .NET version of this article, see 300929.
For a Microsoft Visual C++ .NET version of this article, see 815653.
This article refers to the following Microsoft .NET Framework Class Library namespaces:
  • System.Xml
  • System.Xml.Xsl
Expand all | Collapse all

On This Page

Summary

This step-by-step article shows you how to apply an Extensible Stylesheet Language (XSL) Transformation (XSLT) to an Extensible Markup Language (XML) document by using the XslTransform class to create a new XML document. XSL is an XML-based language that is designed to transform one XML document into another XML document or an XML document into any other structured document.

Requirements

This list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Microsoft Visual Studio 2008, Microsoft Visual Studio 2005, or Microsoft Visual Studio .NET.
  • Microsoft .NET SDK QuickStarts
This article assumes that you are familiar with the following topics:
  • XML terminology
  • Creating and reading an XML file
  • XML Path Language (XPath) syntax
  • XSL

Steps to build the sample

This example uses two files that are named Books.xml and Books.xsl. You can create your own Books.xml and Books.xsl files or use the sample files that are included with the .NET Software Development Kit (SDK) QuickStarts. You must copy the Books.xml and Books.xsl files to the Bin\Debug folder that is located underneath the folder in which you create this project. These files can be found in the following folder:
..\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Samples\QuickStart\Howto\Samples\Xml\Transformxml\Cs
  1. Create a new C# console application in Visual Studio 2008 or in an earlier version of Visual Studio.
  2. Make sure that the project contains a reference to the System.Xml namespace, and add a reference if it does not.
  3. Specify the using statement on the Xml and Xsl namespaces so that you are not required to qualify declarations in those namespaces later in your code. You must use the using statement prior to any other declarations.
    using System.Xml;
    using System.Xml.Xsl;
    					
  4. Declare the appropriate variables, and declare an XslTransform object to transform XML documents.
    XslTransform myXslTransform;
    					
  5. Construct a new XslTransform object. The XslTransform class is an XSLT processor that implements the XSLT version 1.0 recommendation.
    myXslTransform = new XslTransform();
    					
  6. Use the Load method to load the XslTransform object with the style sheet. This style sheet transforms the details of the Books.xsl file into a simple ISBN list of books.
    myXslTransform.Load("books.xsl")
    					
  7. Call the Transform method to initiate the transformation, passing in the source XML document and the transformed XML document name.
    myXslTransform.Transform("books.xml", "ISBNBookList.xml");
    					
  8. Build and then run your project. You can find the resultant ISBNBookList.xml file in the Bin\Debug folder under your project file's folder.

Complete code sample

using System;
using System.Xml;
using System.Xml.Xsl; 
namespace XSLTransformation
{
    /// Summary description for Class1.
    class Class1
    {
        static void Main(string[] args)
        {
            XslTransform myXslTransform; 
            myXslTransform = new XslTransform();
            myXslTransform.Load("books.xsl"); 
            myXslTransform.Transform("books.xml", "ISBNBookList.xml"); 

        }
    }
}
				

References

For more information about the XslTransform class with the XslTransform object, visit the following MSDN Web site:
XslTransform Class
For more information about XML in .NET, see the "XML in .NET: .NET Framework XML Classes and C# Offer Simple, Scalable Data Manipulation" article from MSDN Magazine. To do this, visit the following MSDN Web site:
http://msdn.microsoft.com/msdnmag/issues/01/01/xml/default.aspx
 

Properties

Article ID: 307322 - Last Review: November 15, 2012 - Revision: 8.0
Applies to
  • Microsoft Visual C# 2008 Express Edition
  • Microsoft Visual C# 2005
  • Microsoft Visual C# .NET 2002 Standard Edition
Keywords: 
kbhowtomaster KB307322

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com