Thursday, March 29, 2012

Performance of Xsl Transformations

Folks, I'm running into some performance issues with my Xsl transformations.
I've done a ton of debugging and digging around, and have come to the
conclusion that the performance issues are NOT caused by slow stored
procedures, or bad XSL/Ts.

I came to this conclusion by doing a test transformation in client-side code
instead of server side aspx, as shown at the bottom of this post. The
transformations were super-fast. However, the client-side javascript isn't a
viable solution for my application.

I'm sure that different versions of the XMLDOM are being used when the code
is client-side as opposed to using System.Xml.Xsl.

Here's my C# code. What I'm trying to do is write out an HTML file of my
transformation.

// Create a FileStream to write with
System.IO.FileStream fs = new System.IO.FileStream(exportPath,
System.IO.FileMode.Create);
// Create an XmlTextWriter for the FileStream
System.Xml.XmlTextWriter oXmlTextWriter = new
System.Xml.XmlTextWriter(fs, System.Text.Encoding.Unicode);

try
{
// Set up the XmlResolver
XmlUrlResolver oXmlUrlResolver = new XmlUrlResolver();
oXmlUrlResolver.Credentials = CredentialCache.DefaultCredentials;

// Set up the XslTransform
System.Xml.Xsl.XslTransform oXslTransform = new
System.Xml.Xsl.XslTransform();
oXslTransform.Load(MyXslPath, oXmlUrlResolver);

// Perform Transformation
XmlDataDocument oXmlDataDocument = new XmlDataDocument(MyDataSet);
oXslTransform.Transform(oXmlDataDocument, null, oXmlTextWriter,
oXmlUrlResolver);

// Clean up
oXmlTextWriter.Close();

return exportPath; // defined elsewhere
}
catch (Exception ex)
{
oXmlTextWriter.Close();
System.IO.File.Delete(exportPath);
ExceptionManager.Publish(ex);
throw(ex);
}

The code works, but it's slow. It was suggested that I use an XPathDocument
instead of an XmlDataDocument. How would I do that?
Any suggestions? Thank You

Client-Side Transformation
<%
var sXml = "MyXml.Xml"
var sXsl = "MyXsl.xsl"

var oXmlDoc = Server.CreateObject("MICROSOFT.XMLDOM");
var oXslDoc = Server.CreateObject("MICROSOFT.XMLDOM");
oXmlDoc.async = false;
oXslDoc.async = false;
oXmlDoc.load(Server.MapPath(sXml));
oXslDoc.load(Server.MapPath(sXsl));
Response.Write(oXmlDoc.transformNode(oXslDoc));
%Ok, I figured this out ... I would like to do some research to figure out
why this is faster (MUCH faster).

The slow way:

DataSet oDataSet = populate dataset with some report data
System.IO.FileStream oFileStream = new System.IO.FileStream(exportPath,
System.IO.FileMode.Create);
System.Xml.XmlTextWriter oXmlTextWriter = new
System.Xml.XmlTextWriter(oFileStream, System.Text.Encoding.Unicode);
try
{
XmlUrlResolver oXmlUrlResolver = new XmlUrlResolver();
oXmlUrlResolver.Credentials = CredentialCache.DefaultCredentials;
System.Xml.Xsl.XslTransform oXslTransform = new
System.Xml.Xsl.XslTransform();
oXslTransform.Load(_XslPath, oXmlUrlResolver);
XmlDataDocument oXmlDataDocument = new XmlDataDocument(oDataSet);
oXslTransform.Transform(oXmlDataDocument, null, oXmlTextWriter,
oXmlUrlResolver);
oXmlTextWriter.Close();

return exportPath; // path of exported file
}
catch (Exception ex)
{
oXmlTextWriter.Close();
System.IO.File.Delete(exportPath);
throw(ex);
}

The fast way:

DataSet oDataSet = populate dataset with some report data
XmlTextReader oXmlTextReader = new XmlTextReader(oDataSet.GetXml(),
XmlNodeType.Document, null);
System.IO.FileStream oFileStream = new System.IO.FileStream(exportPath,
System.IO.FileMode.Create);
System.Xml.XmlTextWriter oXmlTextWriter = new
System.Xml.XmlTextWriter(oFileStream, System.Text.Encoding.Unicode);
try
{
XmlUrlResolver oXmlUrlResolver = new XmlUrlResolver();
oXmlUrlResolver.Credentials = CredentialCache.DefaultCredentials;
System.Xml.Xsl.XslTransform oXslTransform = new
System.Xml.Xsl.XslTransform();
oXslTransform.Load(_XslPath, oXmlUrlResolver);
XPathDocument oXPathDocument = new XPathDocument(oXmlReader);
oXslTransform.Transform(oXPathDocument, null, oXmlTextWriter,
oXmlUrlResolver);
oXmlTextWriter.Close();

return exportPath; // path of exported file
}
catch (Exception ex)
{
oXmlTextWriter.Close();
System.IO.File.Delete(exportPath);
throw(ex);
}

"George Durzi" <gdurzi@.hotmail.com> wrote in message
news:#eQPvdwzDHA.1744@.TK2MSFTNGP12.phx.gbl...
> Folks, I'm running into some performance issues with my Xsl
transformations.
> I've done a ton of debugging and digging around, and have come to the
> conclusion that the performance issues are NOT caused by slow stored
> procedures, or bad XSL/Ts.
> I came to this conclusion by doing a test transformation in client-side
code
> instead of server side aspx, as shown at the bottom of this post. The
> transformations were super-fast. However, the client-side javascript isn't
a
> viable solution for my application.
> I'm sure that different versions of the XMLDOM are being used when the
code
> is client-side as opposed to using System.Xml.Xsl.
> Here's my C# code. What I'm trying to do is write out an HTML file of my
> transformation.
> // Create a FileStream to write with
> System.IO.FileStream fs = new System.IO.FileStream(exportPath,
> System.IO.FileMode.Create);
> // Create an XmlTextWriter for the FileStream
> System.Xml.XmlTextWriter oXmlTextWriter = new
> System.Xml.XmlTextWriter(fs, System.Text.Encoding.Unicode);
> try
> {
> // Set up the XmlResolver
> XmlUrlResolver oXmlUrlResolver = new XmlUrlResolver();
> oXmlUrlResolver.Credentials = CredentialCache.DefaultCredentials;
> // Set up the XslTransform
> System.Xml.Xsl.XslTransform oXslTransform = new
> System.Xml.Xsl.XslTransform();
> oXslTransform.Load(MyXslPath, oXmlUrlResolver);
> // Perform Transformation
> XmlDataDocument oXmlDataDocument = new XmlDataDocument(MyDataSet);
> oXslTransform.Transform(oXmlDataDocument, null, oXmlTextWriter,
> oXmlUrlResolver);
> // Clean up
> oXmlTextWriter.Close();
> return exportPath; // defined elsewhere
> }
> catch (Exception ex)
> {
> oXmlTextWriter.Close();
> System.IO.File.Delete(exportPath);
> ExceptionManager.Publish(ex);
> throw(ex);
> }
>
> The code works, but it's slow. It was suggested that I use an
XPathDocument
> instead of an XmlDataDocument. How would I do that?
> Any suggestions? Thank You
>
> Client-Side Transformation
> <%
> var sXml = "MyXml.Xml"
> var sXsl = "MyXsl.xsl"
> var oXmlDoc = Server.CreateObject("MICROSOFT.XMLDOM");
> var oXslDoc = Server.CreateObject("MICROSOFT.XMLDOM");
> oXmlDoc.async = false;
> oXslDoc.async = false;
> oXmlDoc.load(Server.MapPath(sXml));
> oXslDoc.load(Server.MapPath(sXsl));
> Response.Write(oXmlDoc.transformNode(oXslDoc));
> %>

0 comments:

Post a Comment