Transforming Xml using an Xsl stylesheet in .net 2

April 12, 2007

Here is a simple way to transform an xml document using xsl in .net 2.0. Note that the class name has changed from XslTransform to XslCompiledTransform between .net 1.1 and .net 2.0.

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlDocumentAsString);
XslCompiledTransform xslTransform = new XslCompiledTransform();
System.Text.StringBuilder builder = new System.Text.StringBuilder();
xslTransform.Load(xslFilename);
System.IO.StringWriter writer = new System.IO.StringWriter(builder);
xslTransform.Transform(xmlDoc, new XmlTextWriter(writer));
writer.Flush();
string transformedString = builder.ToString();