After a few days of trying to figure out how to manipulate XML with XSLT stylesheets in C#, I discovered it was slightly difficult. I personally don’t like difficult things remaining difficult, and promptly work on attempting to make them… less difficult.

If anybody wants to know how to apply XSLT to XML – I have included a class that is quite simple to use. Note though that it has only been used for my own purposes, and thus only tested for those purposes. If you do find bugs, or have xml/xslt that it does not work for, please send me examples of failures (if you are allowed) and I would be happy to fix any bugs.


If you have an XML document with processing instructions embedded:

< ?xml-stylesheet type="text/xsl" href="http://pathtoxslsheethere" ?>

Then usage is extremely simple:

 // optionally you can use XmlDocument:  XmlDocument doc = new XmlDocument();  and then load up the document
 XmlTransformer xmlToTransform = new XmlTransformer(doc,
      XsltAllow.DocumentFunction | XsltAllow.DtdProcessing | XsltAllow.ExternalResources);    
 //You can also explicitly set the settings in the properties of XmlTransformer
 string theTransformedDocument= xmlToTransform .ApplyTransformation();  
//Alternatively, you can pass in XSL or a URI to XSL or an XmlDocument object that has loaded an XSLT stylesheet.

If you do not have an XML document with processing instructions, the only part of the usage that changes is that you must add in a link/string/xmldocument pointing to or containing your XSLT to apply in the ApplyTransformation(); function.