Does anyone (please) know how to do this? I thought that there would be an easy way to achieve this but can't find anything about saving the contents of WebBrowser HTML.
Asked
Active
Viewed 9,403 times
2 Answers
24
You might try something like this:
(Assuming C# 4 and WPF 4)
dynamic doc = webBrowser.Document;
var htmlText = doc.documentElement.InnerHtml;
Works for me...

kmontgom
- 1,419
- 13
- 18
-
I searched everywhere for a simple solution and this is it! Brilliant! No need to use COM or reference any other libraries. – Ryan Andres Oct 12 '11 at 05:43
-
will that save any related images as well or not ? – Bek Raupov May 18 '12 at 12:26
-
1This code throws this exception for me: 'System.__ComObject' does not contain a definition for 'documentElement' Any thoughts ? – Alexandru Dicu May 15 '21 at 10:21
-3
Yous should use HttpWebRequest and HttpWebResponse objects. Simple sample (found in web, tested, working):
HttpWebRequest myWebRequest = (HttpWebRequest)HttpWebRequest.Create(@"http://www.[pagename].com");
myWebRequest.Method = "GET";
HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
StreamReader myWebSource = new StreamReader(myWebResponse.GetResponseStream());
string myPageSource = string.Empty;
myPageSource = myWebSource.ReadToEnd();
myWebResponse.Close();

zgorawski
- 2,597
- 4
- 30
- 43
-
I don't think that's what I need as I am altering the HTML in the WebBrowser control (adding new divs, form controls). I need to save the HTML directly from the control, not from a URL. – dgwyer Oct 12 '10 at 14:11