Click here to Skip to main content
15,905,614 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Below is the c# code i use to alter the html displayed in IE. However it always throws exception - Could not set the outerHTML property. Invalid target element for this operation. Is it not possible to set the outerHTML?

C#
protected void AlterContent(ref HTMLDocument docInput, HTMLDocument docAlteredOutPut)
{
    try
    {
        if (docInput.body.tagName.ToLower() == "body" && docAlteredOutPut.body.innerHTML != null)
        {
            docInput.documentElement.outerHTML = docAlteredOutPut.documentElement.outerHTML;
        }
    }
    catch (Exception ex)
    {
    }
}


Thanks, Arun
Posted
Updated 8-May-11 1:59am
v2
Comments
Ed Nutting 8-May-11 8:00am    
Edit: Only put your code in a pre (code) block, not your question as well. Also, try not to use slang such as 'Aint'. Thanks, Ed :)
Keith Barrow 8-May-11 8:53am    
HtmlDocument doesn't have a documentElement property.
Are you using a 3rd party class? I notice you are using HTMLDocument (is the capitialisation a typo?)
Arun Chembilath 8-May-11 9:04am    
Its not a typo. The interface mshtml.HTMLDocument. It has the documentElement property.
Keith Barrow 8-May-11 9:57am    
Ah, that explains it, never had to use mshtml, possibly a good thing given the lack of resources!

1 solution

Yes, it is possible, but look at what you're trying to do.
You pick the document Element, which is already the most "outer" in the document. Your can only take or write a whole different document.

[EDIT]

In this case you can only to create a document from scratch and start over. The class System.Windows.Forms.HtmlDocument does not have constructors or other means of explicit instantiating. You can obtain empty document via its instance of System.Windows.Forms.WebBrowser. Navigate to (Uri)null and you will get clean document when the event System.Windows.Forms.WebBrowser.Navigated is handled (important!). The property System.Windows.Forms.WebBrowser.Document will return empty document which you can populate.

Example:
C#
bool startFromScratch = false;

//...

startFromScratchButton.Click += (sender, eventArgs) => {
   startFromScratch = true;
   webBrowser.Navigate((Uri)null); //navigate to empty   
} //startFromScratchButton.Click

webBrowser.Navigated += (sender, eventArgs) => {
   if (startFromScratch) {
      var document = webBrowser.Document;
      var body = document.Body;
      document.Body.InnerHtml = "<p>some paragraph content</p>";
   } //if
}; //webBrowser.Navigated


I tested it.

—SA
 
Share this answer
 
v4
Comments
Arun Chembilath 8-May-11 11:34am    
I'm sorry.. I'm pretty new to this so could you please explain?
Sergey Alexandrovich Kryukov 8-May-11 12:01pm    
I don't know what else to explain...
I can only repeat: "outer" is not applicable to all elements. If the element is already outermost (is it more clear?), what is its "outer"? Nothing.
--SA
Arun Chembilath 8-May-11 12:03pm    
Okie I did get that but again its not possible to assign the documentElement. It is readonly
Sergey Alexandrovich Kryukov 8-May-11 12:37pm    
OK, so what's the problem? Don't assign it. What do you need to achieve?
--SA
Arun Chembilath 8-May-11 12:49pm    
I need to change the HTML content of the docInput above with the HTML content in docAlteredOutPut. I could replace the innerHTMl but I want to do the whole HTMl.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900