Tutorial :Converting log files to XML and (X)HTML, recommendedations


I've been tasked with converting some text log files from a test reporting tool that I've inherited. The tool is a compiled C# (.NET 3.5) application.

I want to parse and convert a group of logically connected log files to a single XML report file, which is not a problem. The System.Xml classes are easy enough to use.

However, I also want to create a more "readable" file to accompany each report. I've chosen HTML, and because I like standardization, I'd prefer to do it in proper XHTML.

My question is how should I go about creating the HTML files along with the XML reports? My initial thought is to build the XML file, then to use LINQ and a simple StreamWriter to build an HTML file within my C# code. I could also use XSLT as opposed to LINQ to make the C# code easier. But since I have to compile this anyway, I don't like the idea of adding more files to the installation/distribution.

Is using LINQ going to cause me any problems as opposed to XSLT? Are there any nice HTML writing libraries for .NET that conform to XHTML? Since I have everything parsed from the log files in working memory, is there an easy way to create both files at the same time easily?


I'd create an xslt transform and just run that against the XML. Linq really isn't designed to transform XML of one schema (e.g., your report) to another (e.g., xhtml). You could brute force it, but xslt is an elegant way to do it.


I would actually recommend using XSL transform. Since you already have the XML doc. If you write a good XSL transform you will get very good results.


small snippet:

    XslCompiledTransform xsl = new XslCompiledTransform();          xsl.Load(HttpContext.Current.Server.MapPath(xslPath));        StringBuilder sb = new StringBuilder();      using (TextWriter tw = new StringWriter(sb))      {          // Where the magic happens          xsl.Transform(xmlDoc, null, tw);              //return of text which you could save to file...          return sb.ToString();      }  


One nice thing with using the XSLT is that you can put a processing instruction at the top of the XML that tells the user's browser how to generate the report itself:

   <?xml-stylesheet type="text/xsl" href="Your_XSLT.xslt"?>  

That way you don't have to have a separate step to generate the report. The user just opens the XML file directly in the browser, but they see the generated report instead.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »