Transforming Xml using an Xsl stylesheet in .net 2

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();

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: