Microsoft KB Archive/307494

= How to apply an XSL transformation to XML for streaming by using Visual C# =

Article ID: 307494

Article Last Modified on 1/11/2007

-

APPLIES TO


 * Microsoft Visual C# 2005
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q307494



For a Microsoft Visual Basic .NET version of this article, see 300934.



For a Microsoft Visual C++ .NET version of this article, see 815655.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Xml
 * System.Xml.Xsl
 * System.Xml.XPath
 * System.IO

IN THIS TASK

 * SUMMARY
 * Requirements
 * Description of XSL Transformation to XML for Streaming
 * Steps to Build the Sample
 * REFERENCES



SUMMARY
This step-by-step article shows you how to apply the Extensible Stylesheets Language (XSL) Transformations (XSLT) language 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 either an XML document into another XML document or an XML document into any other structured document.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Visual Studio .NET or Microsoft Visual Studio 2005
 * Microsoft Visual Studio .NET Software Development Kit (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

back to the top

How to Apply XSL Transformations
This example uses two files 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

 Create a new Console Application in Visual C#. Make sure that the project contains a reference to the System.Xml namespace, and add a reference if it does not.  Use the using statement on the Xml, XPath, 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; using System.Xml.XPath;   Declare the appropriate variables. Declare an XPathDocument object to hold the XML document and an XslTransform object to transform XML documents. Add the declaration code in the Main procedure of Module1: XslTransform myXslTransform; XPathDocument myXPathDocument;   Populate an XPathDocument object with the sample file, Books.xml. The XPathDocument class provides a fast and performance-oriented cache to process XML documents by using XSLT. The XPathDocument class is similar to an XML Document Object Model (DOM) but is highly optimized for XSLT processing and the XPath data model: myXPathDocument = new XPathDocument (&quot;books.xml&quot;);   Construct a new XslTransform object. The XslTransform class is an XSLT processor that implements the XSL Transformations (XSLT) version 1.0 recommendation: myXslTransform = new XslTransform; </li>  Use the Load method to load the XslTransform object with the style sheet. This style sheet transforms the details of the Books.xsl document into a simple International Standard Book Number (ISBN) list of books: myXslTransform.Load(&quot;books.xsl&quot;); </li>  Create an XmlTextWriter class with the new, transformed XML file name. Call the Transform method to initiate the transformation. XmlTextWriter writer = new XmlTextWriter(&quot;ISBNBooks.xml&quot;,System.Text.Encoding.UTF8); myXslTransform.Transform(myXPathDocument,null, writer); writer.Flush; writer.Close; </li>  Alternately, you can send the transformed XML document to an XmlReader, Stream, or TextWriter class. The following code sample sends the XML transformation to an instance of the StringWriter (a derivative of TextWriter), which in turn writes the transformation to the console window. System.IO.StringWriter stWrite = new System.IO.StringWriter; myXslTransform.Transform(myXPathDocument, null, stWrite); Console.WriteLine(stWrite.ToString); Console.ReadLine; NOTE: The complete code listing uses the preceding code instead of the code in step 8. </li>  Build and run your project. The results of the transformation are displayed in the console window as follows: <pre class="fixed_text">   <book ISBN=&quot;1-861003-11-0&quot;> 8.99 <book ISBN=&quot;0-201-63361-2&quot;> 11.99 <book ISBN=&quot;1-861001-57-6&quot;> 9.99 </li></ol>

back to the top

Complete Code Sample
using System; using System.Xml; using System.Xml.Xsl; using System.Xml.XPath; using System.IO;

namespace XSLTransformfromXPath {   /// Summary description for Class1. class Class1 {       static void Main(string[] args) {           XslTransform myXslTransform; XPathDocument myXPathDocument = new XPathDocument (&quot;books.xml&quot;); myXslTransform = new XslTransform; myXslTransform.Load(&quot;books.xsl&quot;); XmlTextWriter writer = new XmlTextWriter(&quot;ISBNBooks.xml&quot;,System.Text.Encoding.UTF8);

myXslTransform.Transform(myXPathDocument,null, writer); writer.Flush; writer.Close;

System.IO.StringWriter stWrite = new System.IO.StringWriter; myXslTransform.Transform(myXPathDocument, null, stWrite); Console.WriteLine(stWrite.ToString); Console.ReadLine;

}   } } back to the top