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?
ant84Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vo1dCommented:
it can be edit.
0
ant84Author Commented:
Thanks i'll give it a go and let you know how i get on.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ant84Author Commented:
Many thanks for your help, vo1d.  That was just the trick and I can now reliably edit the <head> section.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.