Microsoft KB Archive/318580

= PRB: XSL Transformations with XmlDataDocument May Perform More Slowly Than XPathDocument =

Article ID: 318580

Article Last Modified on 9/22/2003

-

APPLIES TO


 * Microsoft .NET Framework 1.0

-



This article was previously published under Q318580



SYMPTOMS
XSL transformations (XSLT) with System.Xml.XmlDataDocument may run more slowly than XSLT with XPathDocument or XmlDocument.



CAUSE
XmlDataDocument is primarily designed to act as the bridge between the DataSet and XML.



RESOLUTION
Run XSL transformations by using the XmlDocument or XPathDocument classes instead of XmlDataDocument. Microsoft recommends that you use XPathDocument, because it is highly optimized for XSL transformations. See the &quot;More Information&quot; section of this article for a code sample that uses the XPathDocument.



STATUS
This behavior is by design. The XmlDataDocument class is primarily designed for synchronizing XML with DataSet. Microsoft is looking into improving its performance for transformations in the next major release of Visual Studio .NET.



MORE INFORMATION
The following is a Visual Basic .NET code sample that shows you how to apply XSL transformations on a DataSet by using the XPathDocument class. The sample initially loads the data from the Customers table and the Order table of the Northwind database into to a DataSet, and then the DataSet is transformed with the XmlDataDocument and XPathDocument classes.

Steps to Reproduce the Behavior
 Create a new Visual Basic .NET Console application project.  Copy and paste the following into a file, and then save the file as Transform.xsl in the bin directory of the application: 

   BODY {font-family:verdana;font-size:9pt} TD  {font-size:8pt}   </BODY> </HTML> </xsl:template>

<xsl:template match=&quot;Customers&quot;> <TR><TD> <xsl:value-of select=&quot;ContactName&quot;/>, <xsl:value-of select=&quot;Phone&quot;/><BR/> </TD></TR> <xsl:apply-templates select=&quot;Orders&quot;/> </xsl:template>

<xsl:template match=&quot;Orders&quot;> </xsl:template>

</xsl:stylesheet> </li>  Replace the code in Module1.vb file with the following. Imports System.IO Imports System.Data Imports System.Data.SqlClient Imports System.Xml Imports System.Xml.Xsl Imports System.Xml.XPath

Module Module1

Sub Main

Try ' Fill a DataSet with data from Customers and Orders tables of northwind database Dim nwindConn As SqlConnection = New SqlConnection(&quot;Password=;User ID=sa;Initial Catalog=Northwind;Data Source=localhost&quot;) nwindConn.Open

Dim myDataSet As DataSet = New DataSet(&quot;CustomerOrders&quot;) Dim custDA As SqlDataAdapter = New SqlDataAdapter(&quot;SELECT * FROM Customers&quot;, nwindConn)

custDA.Fill(myDataSet, &quot;Customers&quot;)

Dim ordersDA As SqlDataAdapter = New SqlDataAdapter(&quot;SELECT * FROM Orders&quot;, nwindConn) ordersDA.Fill(myDataSet, &quot;Orders&quot;) nwindConn.Close

myDataSet.Relations.Add(&quot;CustOrders&quot;, _                                myDataSet.Tables(&quot;Customers&quot;).Columns(&quot;CustomerID&quot;), _                                 myDataSet.Tables(&quot;Orders&quot;).Columns(&quot;CustomerID&quot;)).Nested = True ' Load the stylesheet Dim xslTran As XslTransform = New XslTransform xslTran.Load(&quot;transform.xsl&quot;) ' Create a writer to stream the results to a file Dim writer As XmlTextWriter = New XmlTextWriter(&quot;xslt_output.html&quot;, System.Text.Encoding.UTF8)

' I. Transform the DataSet data with XmlDataDocument Dim startTime As DateTime ' Synchronize an XmlDataDocument with the DataSet Dim xmlDataDoc As XmlDataDocument = New XmlDataDocument(myDataSet) startTime = DateTime.Now xslTran.Transform(xmlDataDoc, Nothing, writer) Console.WriteLine(&quot;Time taken with XmlDataDocument: &quot; + ((DateTime.Now).Subtract(startTime)).TotalSeconds.ToString)

' II. WORKAROUND: Use XPathDocument to transform the DataSet data Dim xpathDoc As XPathDocument = New XPathDocument(New StringReader(myDataSet.GetXml)) startTime = DateTime.Now xslTran.Transform(xpathDoc, Nothing, writer) Console.WriteLine(&quot;Time taken with XPathDocument : &quot; + ((DateTime.Now).Subtract(startTime)).TotalSeconds.ToString + vbNewLine) Console.ReadLine writer.Close

Catch e As Exception Console.WriteLine(&quot;{0}&quot;, e.Message) End Try

End Sub End Module </li> Modify the connection string to suit your database settings. Compile and run the application. Read the inline comments to understand the functionality of the code.</li></ol>

<div class="references_section">