Illustrates the XslTransform class : XML Transform « XML « C# / C Sharp

Illustrates the XslTransform class

Illustrates the XslTransform class
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110

  Example20_3.cs illustrates the XslTransform class

using System;
using System.Xml;
using System.Xml.Xsl;
using System.IO;

public class Example20_3 

    public static void Main() 

        // use an XmlTextReader to open an XML document
        XmlTextReader xtr = new XmlTextReader("Cust3.xml");
        xtr.WhitespaceHandling = WhitespaceHandling.None;

        // load the file into an XmlDocuent
        XmlDocument xd = new XmlDocument();
        // load an XSLT file
        XslTransform xslt = new XslTransform();

        // perform the transformation in memory
        MemoryStream stm = new MemoryStream();
        xslt.Transform(xd, null, stm);

        // and dump the results
        stm.Position = 1;
        StreamReader sr = new StreamReader(stm);

        // close the reader


<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Cust.xsl"?>
        <CompanyName>Alfreds Futterkiste</CompanyName>
        <ContactName>Maria Anders</ContactName>
        <ContactTitle>Sales Representative</ContactTitle>
        <Address>Obere Str. 57</Address>
        <CompanyName>A Company</CompanyName>
        <ContactName>Laurence Lebihan</ContactName>
        <Address>12, rue des Bouchers</Address>


<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="">
    <xsl:for-each select="/NewDataSet/Customers">
         <br><b><xsl:value-of select="CustomerID"/></b></br>
         <br><xsl:value-of select="CompanyName"/></br>
         <br><xsl:value-of select="ContactName"/></br></p>



Related examples in the same category

1.Use XslCompiledTransform
2.Perform an XSL Transform
3.Read command line input and do the xml xsl translation
4.XML transformation: transform XML file to HTML file
5.Perform an XSL Transformation using XML and XSL content and with some XSLT arguments
6.Xslt Transformation Helper