tags:

views:

176

answers:

4

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?

+4  A: 

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.

Will
Thanks, I'll go with XSLT, then. It sounds from all the answers to certainly be the best solution.
cgyDeveloper
+4  A: 

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.

http://www.w3schools.com/xsl/xsl_transformation.asp

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();
    }
BigBlondeViking
I assume, then, that the transform method is just putting the stylesheet header into the XML file? Is that right?
cgyDeveloper
Not in my code snippet i provided was from an email util that takes and xml data and transforms it into html to be injected into the body of an email.
BigBlondeViking
Depends on your goal? if you want the end user to do the transform, then the header will work fine... but sometimes you might want to just process it 1 time and send out the html... depends on you use cases
BigBlondeViking
Okay, I see. I can choose to have the stylesheet link right in the XML or actually convert to a standalone HTML.
cgyDeveloper
exactly, either way you will need to write a good xsl, which will be the hard part... but once you do... its a cake walk :)
BigBlondeViking
+2  A: 

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.

David
A: 

xsl.Load(HttpContext.Current.Server.MapPath(xslPath));

this shows like httpContext is not included as an exception... what this xslpath indicate, please explain me clearly as i am new to this type conversion

sudha