• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1782
  • Last Modified:

MSHTML edit the <head> section of a document

I have an mshtml.IHTMLDocument which I would like to add a new stylesheet to.

The only way to add a stylesheet is in the <head> section, which it seems I cannot edit in anyway.  

Does anyone have any recommendations on how it can be done?
0
ant84
Asked:
ant84
  • 4
  • 4
1 Solution
 
vo1dCommented:
it can be edit.
0
 
ant84Author Commented:
Thanks i'll give it a go and let you know how i get on.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ant84Author Commented:
That looks like it'll do the trick, i am running into a couple of problems though with finding a method to edit the inner html of the <style> node.  Is there such a way you can think of?  
0
 
ant84Author Commented:
Here is my code:


            mshtml.HTMLDocument htmlDocument;

            htmlDocument = (mshtml.HTMLDocument)this.wbr2.Document;

            foreach (mshtml.IHTMLDOMNode node in (htmlDocument.childNodes as mshtml.IHTMLDOMChildrenCollection))
            {
                ProcessNode(node);
            }

Then:

        private void ProcessNode(mshtml.IHTMLDOMNode node)
        {            
            if (node is mshtml.HTMLHeadElement)
            {
                foreach (mshtml.IHTMLDOMNode cNode in (node.childNodes as mshtml.IHTMLDOMChildrenCollection))
                {
                    //there doesn't seem to be a cNode.innerHTML property
                }


                //it doesnt let me instatiate a new mshtml.IHTMLDOMNode like this!

                mshtml.IHTMLDOMNode newNode = new mshtml.IHTMLDOMNode();
                ((mshtml.HTMLLinkElement)newNode).setAttribute("rel", "stylesheet", 0);
                ((mshtml.HTMLLinkElement)newNode).setAttribute("href", "stylesheets/paviors.css", 0);

                node.appendChild((mshtml.IHTMLDOMNode)newNode);

            }
            if ((node.childNodes as mshtml.IHTMLDOMChildrenCollection).length == 0)
            {
                return;
            }
            foreach (mshtml.IHTMLDOMNode childNode in (node.childNodes as mshtml.IHTMLDOMChildrenCollection))
            {
                ProcessNode(childNode);
            }
        }

I raised the points on this question, I've been experimenting for much of the day and still can't get it working
0
 
vo1dCommented:
maybe this works for you:

private void axWebBrowser1_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
        {
            mshtml.HTMLDocument htmlDocument = (mshtml.HTMLDocument)this.axWebBrowser1.Document;

            foreach (mshtml.IHTMLStyleSheet styleSheet in (htmlDocument.styleSheets as mshtml.HTMLStyleSheetsCollection))
            {
                 //check the styleSheet ref here
            }            
        }

in this case i think, the ProcessNode method will not fit to your needs.
another problem could be, that the webpage is loaded and remains in memory, from were it will be rendered in the browser.
so it could be, that you will not see the changes of your stylesheet.
therefore i would recommend, to temporary save the modified document and load it again in the browser.
but therefore, you will need a flag to check, in which state you are.
maybesomething like this:
private bool loadTempDocument = false;

if you have modified your document with your stylesheet, save it to your harddisk, set the flag to true, load the saved document again and set the flag back to false.
0
 
vo1dCommented:
so the code would look like this:

private bool loadTempDocument = false; //<-- this field as a classfield

private void axWebBrowser1_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
{
    mshtml.HTMLDocument htmlDocument = (mshtml.HTMLDocument)this.axWebBrowser1.Document;

    if((loadTempDocument = !loadTempDocument))
    {
        foreach (mshtml.IHTMLStyleSheet styleSheet in (htmlDocument.styleSheets as mshtml.HTMLStyleSheetsCollection))
        {
           //check and modify the styleSheet ref here                  
        }
        //save the whole modified document to disk      
        //load the modified document from disk
    }  
}
0
 
ant84Author Commented:
Many thanks for your help, vo1d.  That was just the trick and I can now reliably edit the <head> section.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now