Generate HTML file from XML using XSLT

August 25, 2014 by Anuraj

.Net .Net 4.0 ASP.Net CodeProject Windows Forms

Recently I had to work on FxCop, for static code analysis. For those who don’t know what is FxCop, check this link. But one issue I faced was, the report can be saved / exported from FxCop is only in XML format. I searched for tool which help me to convert this XML to any other formats like HTML or doc, but I couldn’t find anything. :( Then I found FxCop comes with some XSL file, which help you to convert the FxCop generated XML to HTML. You can find these XSL files under - “C:\Program Files (x86)\Microsoft Fxcop 10.0\Xml” folder. And here is the code which helps to generate HTML using XML and XSLT.

public static string TransformXMLToHTML(string xmlFile, string xsltFile)
{
    var transform = new XslCompiledTransform();
    using (var reader = XmlReader.Create(File.OpenRead(xsltFile)))
    {
        transform.Load(reader);
    }

    var results = new StringWriter();
    using (var reader = XmlReader.Create(File.OpenRead(xmlFile)))
    {
        transform.Transform(reader, null, results);
    }

    return results.ToString();
}

Happy Programming :)

Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub