Quantcast Applying an XSLT Transform to a DataSet

Applying an XSLT Transform to a DataSet

by Neon Quach 24. Tháng Mười Một 2009 04:45

The WriteXml method of the DataSet enables you to write the contents of a DataSet as XML data. A common task is to then transform that XML to another format using XSL Transformations (XSLT). However In .NET, the DataSet is synchronized with the XmlDataDocument which enables you to apply an XSLT stylesheet to the contents of a DataSet without having to first write the contents of the DataSet as XML data using WriteXml.

Let’s get started with small C# and VB.NET console application.

The following example uses a single XSLT stylesheet to transform the XML for the HumanResources.Contact table in the AdventureWorks database into HTML displaying the contact data in an HTML table.

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"

  <xsl:template match="/">



        BODY {font-family:verdana;font-size:12px}

        TD   {font-size:10pt}



        <TABLE BORDER="1">

          <tr bgcolor="#AAAAAA">

            <td>Contact ID</td>


            <td>First Name</td>

            <td>Last Name</td>

            <td>Email Address</td>


          <xsl:for-each select="/ContactDS/Contact">


              <td style="text-align:center">

                <xsl:value-of select="ContactID" />


              <td style="text-align:center">

                <xsl:value-of select="Title" />



                <xsl:value-of select="FirstName" />



                <xsl:value-of select="LastName" />



                <xsl:value-of select="EmailAddress" />











By creating a new style sheet file, named it to Contact.xslt and save it in the same directory as the project file.

            string xsltContactFile = "../../Contact.xslt";

            string strConn = @"Data Source=.\sqlexpress;Initial Catalog=AdventureWorks;Persist Security Info=True;Integrated Security=True;";

            string strComm = "select top 10 ContactID,Title,FirstName,LastName, EmailAddress from Person.Contact";

            using (SqlConnection sqlConn = new SqlConnection(strConn))


                SqlDataAdapter dap = new SqlDataAdapter(strComm, sqlConn);

                DataSet ds = new DataSet("ContactDS");

                dap.Fill(ds, "Contact");

                XmlDataDocument dataDoc = new XmlDataDocument(ds);

                XslCompiledTransform transform = new XslCompiledTransform();


                XmlTextWriter xmlWriter = new XmlTextWriter("../../Contact.html", Encoding.UTF8);


                Console.Write("DataSet transformed to HTML in file " + xsltContactFile + " Successful!");



        Dim xsltContactFile As String = "../../Contact.xslt"

        Dim strConn As String = "Data Source=.\sqlexpress;Initial Catalog=AdventureWorks;Persist Security Info=True;Integrated Security=True;"

        Dim strComm As String = "select top 10 ContactID,Title,FirstName,LastName, EmailAddress from Person.Contact"

        Using sqlConn As New SqlConnection(strConn)

            Dim dap As New SqlDataAdapter(strComm, sqlConn)

            Dim ds As New DataSet("ContactDS")

            dap.Fill(ds, "Contact")

            Dim XmlDataDocument As New XmlDataDocument(ds)

            Dim transform As New XslCompiledTransform()


            Dim xmlWriter As New XmlTextWriter("../../Contact.html", System.Text.Encoding.UTF8)

            transform.Transform(XmlDataDocument, Nothing, xmlWriter)


            Console.Write("DataSet transformed to HTML in file " & xsltContactFile & " Successful!")


        End Using

To use XSLT to transform the contents of a DataSet, create an XslCompiledTransform object and call the Transform() method on that object.

This code above fills a DataSet with a several fields from the TOP 10 rows of the Person.Contact table in the AdventureWorks database. The style sheet Contact.xslt used to transform the DataSet into a HTML file named Contact.html written to the same directory of project file.

Press F5 then you can open Contact.html file you can see the result like:

Applying-an-XSLT-Transform-to-a-DataSet.rar (67.29 kb)

Tags: , , ,

Categories: net framework | c# | vb.net

blog comments powered by Disqus

Powered by BlogEngine.NET - Eco Theme by n3o Web Designers