A WinForm application. I want to scrape a part of an HTML web page and save it into a local html file.
I have one local file, “empty.htm” (containing just “I’m empty” in the body), one remote web page, and two WebBrowser controls. WebBrowser1 navigates to the remote page, WebBrowser2 to the local file. Both display their content appropriately.
Now I try:
string rootIDToCopy = "InterestingDivID";
HtmlDocument htmlDocument = webBrowser1.Document;
HtmlElement rootElementToCopy =
htmlDocument.GetElementById(rootIDToCopy);
if (rootElementToCopy != null)
{
HtmlDocument dest = webBrowser2.Document;
if (dest != null)
{
HtmlElement destBody = dest.Body; // Point 1
destBody.AppendChild(rootElementToCopy); // Point 2
}
}
Now, when I’m in Point 1, I see that destBody exists, has no children and has an InnerHTML of “I’m empty”. rootElementToCopy appears valid (has three children and an ok InnerHtml). However, at Point 2 I get “Value does not fail within the expected range” (probably from Windows.Forms.UnsafeNativeMethods.IHTMLElement2.InsertAdjacentElement).
Help will be appreciated!
You may not be allowed to: see
WRONG_DOCUMENT_ERRandownerDocumentin the DOM specification.Instead I think you might have to serialize the subtree to a flat string format before you try to insert it into a different document.