views:

64

answers:

3

We are supplied with HTML 'wrapper' files from the client, which we need to insert out content into, and then render the HTML.

Before we render the HTML with our content inserted, I need to add a few tags to the <head> section of the client's wrapper, such as references to our script files, css and some meta tags.

So what I'm doing is

string html = File.ReadAllText(wrapperLocation, Encoding.GetEncoding("iso-8859-1"));

and now I have the complete HTML. I then search for a pre-defined content well in that string and insert our content into that, and render it.

How can I create an instance of a HTML document and modify the <head> section as required?

edit: I don't want to reference System.Windows.Forms so WebBrowser is not an option.

A: 

I use the WebBrowser control as host, and navigate/alter the document through its Document property.

Nice documentation and samples at the link above.

Mau
That exists in System.Windows.Forms, so I'm not really too keen on referencing that from a web project..
DaveDev
+1  A: 

I haven't tried this library myself, but this would probably fit the bill: http://htmlagilitypack.codeplex.com/

Justin Pinkley
I played around with this for a while but I couldn't find a way to insert nodes into the HtmlDocument.. and the documentation for HtmlAgilityPack isn't very good!! Thanks anyway
DaveDev
A: 

I couldn't get an automated solution to this, so it came down to a hack:

public virtual void PopulateCssTag(string tags)
{
    // tags is a pre-compsed string containing all the tags I need.
    this.Wrapper = this.Wrapper.Replace("</head>", tags + "</head>");
}
DaveDev