views:

4

answers:

1

I'm creating an html file based on xml and xsl with XslCompiledTransform in c#.net. This works perfectly.

But the xsl also has a css file included, and I'm wondering if there is any way to get this css styles included in the output html file, so it can be showed as a standalone file (so I don't have to copy the css file to wherever i want to see the file).

To define the style of each tag explicitly is not an option either unfortunately, and the file is of course really ugly without the css.

Any help would be very much appreciated! :)

+1  A: 

In your output html add a style sheet link within the <head> tag.

<link rel="stylesheet" type="text/css" href="mystyle.aspx" />

Then add a page to your project called mystyle.aspx. In Page_Load of this file you do your xslt transformation to output only the css part. (And remove the css part of the transformation for the html pages).

protected void Page_Load(object sender, EventArgs e) {
    Response.Clear();
    Response.ContentType = "text/css";

    string css = // Do your xslt transformation here

    Response.Write( css );
    Response.End();
}

If the CSS is the same for all pages, you might want to add some caching to the code above to save doing the transformation every time.

You might have to use some parameters to point to your xml/xslt, but you haven't provided any information in your question in this regard.

Mikael Svenson