Simplest way to transform XML to HTML with XSLT in C#?

How about:

public static string TransformXMLToHTML(string inputXml, string xsltString)
    XslCompiledTransform transform = new XslCompiledTransform();
    using(XmlReader reader = XmlReader.Create(new StringReader(xsltString))) {
    StringWriter results = new StringWriter();
    using(XmlReader reader = XmlReader.Create(new StringReader(inputXml))) {
        transform.Transform(reader, null, results);
    return results.ToString();

Note that ideally you would cache and re-use the XslCompiledTransform – or perhaps use XslTransform instead (it is marked as deprecated, though).

Leave a Comment