The XslCompiledTransform does not support precompiled versions of stylesheets (.Net Framework 2.0), i.e. on each application run, you suffer the compilation hit.
Resolution
The performance hit can be mitigated with the use of caching techniques, namely the Application cache
The code below illustrates a couple of helper methods that take advantage of caching techniques:
private XslCompiledTransform GetXsl(HttpContext ctx, string cacheFileName, string xslfileContents)
{return GetXsl(ctx, cacheFileName, xslfileContents, new XmlUrlResolver());
}
private XslCompiledTransform GetXsl(HttpContext ctx, string cacheFileName, string xslfileContents, XmlResolver xmlres)
{XslCompiledTransform xsldoc = new XslCompiledTransform();xsldoc = (XslCompiledTransform)ctx.Cache.Get(cacheFileName);
if (xsldoc == null)
{xsldoc = new XslCompiledTransform();using (Stream res = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(xslfileContents)))
{using (XmlTextReader stylesheet = new XmlTextReader(res))
{xsldoc.Load(stylesheet, new XsltSettings(true, false), xmlres);
}
}
ctx.Cache.Add(
cacheFileName,
xsldoc,
new System.Web.Caching.CacheDependency(cacheFileName),System.Web.Caching.Cache.NoAbsoluteExpiration,
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Normal,
null);}
return xsldoc;}
References
XslCompiledTransform Performance: Beating MSXML 4.0
0 comments:
Post a Comment
I always welcome feedback from my readers.