My CodeShareWeek contribution
2009-03-13 @ 19:57#
MikeWo declared March 8-14 "CodeShareWeek." my code-mate, CarpDeus posted a few things and i finally figured i'd join in, too.
so here's my contribution to CodeShareWeek. it's an XSLT helper class. i do lots of work w/ XSLT and get tired of rewriting the same functions. over time, i've tweaked this class to do things like:
- Cache the
XmlDocument - Cache the
XslCompiledTransformclass - Return results as a
MemoryStreamor as aString(XmlDocument + StringWriter = EVIL) - Properly handle BOM (byte-order marks are evil, dangit!)
- Use
XPathNavigatorfor fast transforms
here's couple examples of my Xslt.cs class in action:
Amundsen.Utilities.Xslt xslt = new Amundsen.Utilities.Xslt();
System.Xml.Xsl.XsltArgumentList XsltArgs = new System.Xml.Xsl.XsltArgumentList();
XsltArgs.AddParam("run-date", "", System.DateTime.Now);
// test #1
Console.Out.WriteLine("*** using two paths");
Console.Out.Write(xslt.Transform(args[0], args[1], XsltArgs));
// test #2
Console.Out.WriteLine("\n*** using memory stream");
System.IO.MemoryStream ms = new System.IO.MemoryStream();
xslt.Transform(args[0], args[1], XsltArgs, ms);
using (System.IO.StreamReader sr = new System.IO.StreamReader(ms))
{
Console.Out.Write(sr.ReadToEnd());
sr.Close();
}
there are a lot more options. you can download the source code and a sample app from my Codeplex space. feel free to leave me feedback there, too.
oh, yeah. thanks to MikeWo for the cool idea!