You are currently offline, waiting for your internet to reconnect

How to apply an XSL Transformation to XML for streaming by using Visual Basic .NET

This article was previously published under Q300934
This article has been archived. It is offered "as is" and will no longer be updated.
For a Microsoft Visual C# .NET version of this article, see 307494.
For a Microsoft Visual C++ .NET version of this article, see 815655.


This article describes 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


The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:

  • Microsoft Visual Studio .NET
  • 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.
  1. Create a new Console Application in Visual Basic .NET.
  2. Make sure that the project contains a reference to the System.Xml namespace, and add a reference if it does not.
  3. Use the Imports 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 Imports statement prior to any other declarations:
    Imports System.XmlImports System.Xml.XPathImports System.Xml.Xsl					
  4. 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:
    Dim myXslTransform As XslTransformDim myXPathDocument As XPathDocument					
  5. Populate an XPathDocument object with the sample file, Books.xml. The XPathDocument class provides a fast and performance-oriented cache to process XML documents 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.
    'Open the XML.	myXPathDocument = New XPathDocument("books.xml")					
  6. 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()					
  7. 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:
  8. Create an XmlTextWriter class with the new, transformed XML file name. Call the Transform method to initiate the transformation.
    Dim writer as XmlTextWriterwriter = New XmlTextWriter("ISBNBooks.xml",System.Text.Encoding.UTF8)myXslTransform.Transform(myXPathDocument,Nothing, writer)writer.Flush()writer.Close()					
  9. Alternately, you can send the transformed XML document into 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.
    Dim stWrite As System.IO.StringWriter = New System.IO.StringWriter()myXslTransform.Transform(myXPathDocument, Nothing, stWrite)Console.WriteLine(stWrite.ToString)Console.ReadLine() 'Pause						
    NOTE: The complete code listing uses the preceding code instead of the code in step 8.
  10. Build and run your project.The results of the transformation are displayed in the console window as follows:
       <root><bookstore><book ISBN="1-861003-11-0"><price>8.99</price>			</book><book ISBN="0-201-63361-2"><price>11.99</price>			</book><book ISBN="1-861001-57-6"><price>9.99</price>			</book></bookstore></root>					
back to the top

Complete code listing

Imports System.XmlImports System.Xml.XPathImports System.Xml.XslModule Module1    Sub Main()        Dim myXslTransform As XslTransform        Dim myXPathDocument As XPathDocument        myXPathDocument = New XPathDocument("books.xml")        myXslTransform = New XslTransform()        myXslTransform.Load("books.xsl")        Dim stWrite As System.IO.StringWriter = New System.IO.StringWriter()        myXslTransform.Transform(myXPathDocument, Nothing, stWrite)        Console.WriteLine(stWrite.ToString)        Console.ReadLine()    End SubEnd Module 				
back to the top
For more information about the XslTransform class, see the following Microsoft .NET Framework Class Library documentation: For more information about the XslTransform class with the XslTransform object, see the following Microsoft .NET Framework Developer's Guide documentation: For a practical comparison of XSLT and Active Server Pages .NET, see the following MSDN Voices Extreme XML column: For more information about XML in .NET, see the ".NET Framework XML Classes and C# Offer Simple, Scalable Data Manipulation" article from MSDN Magazine at the following Microsoft Web site:  
For more information, refer to the following book:
R. Allen Wyke, Sultan Rehman, Brad Leupen, XML Programming (Core Reference), Microsoft Press, 2001
For more information, refer to the following Microsoft Training & Certification course: back to the top
dotnet xml

Article ID: 300934 - Last Review: 12/06/2015 03:11:52 - Revision: 9.0

Microsoft Visual Basic .NET 2002 Standard Edition, Microsoft Visual Basic .NET 2003 Standard Edition

  • kbnosurvey kbarchive kbhowtomaster KB300934