|
|
The Renegade Blog
|
 |
|
|
|
|
|
|
The Renegade Blog
|
 |
|
|
|
|
 |
|
|
The Renegade Blog
|
 |
|
Dec
27
Written by:
Renegade
Thursday, December 27, 2007 12:00 AM
So I need to get an HTMLDocument from a string, and using the MSHTML DLL is just pure hell as I don't want to fart around with all that and would much rather stay in pure C# and managed code.
Searching around, I find this: http://www.thescripts.com/forum/thread607769.html
1: WebBrowser webControl = new WebBrowser(); 2: webControl.DocumentText = currentHtml; 3: 4: do 5: { 6: Application.DoEvents(); 7: 8: } while (webControl.ReadyState != WebBrowserReadyState.Complete);
Excellent. I put it into my Windows Forms utility that I'm writing and try it out -- It works perfectly... But later on I end up with JavaScript errors when testing more data. :( Not happy.
In any event, I'm wondering if there's something in the textbox control that is also holding the HTML that does this that I'm missing... Nope. Can't find anything. Go back to the WebBrowser control... Got it. Need to add one line there:
1: WebBrowser webControl = new WebBrowser(); 2: webControl.DocumentText = currentHtml; 3: webControl.ScriptErrorsSuppressed = true; // Add in this here 4: 5: do 6: { 7: Application.DoEvents(); 8: 9: } while (webControl.ReadyState != WebBrowserReadyState.Complete);
Nasty JavaScript errors are gone! :)
This happens because the WebBrowser control doesn't actually need to be displayed graphically -- it runs invisibly just as you would expect it to do if it were inside a Windows Forms program.
So, there you have it. A nifty little thing learned today.
Cheers,
Ryan
Tags:
4 comments so far...
Re: Convert String to HTMLDocument in C# Without JavaScript Errors
Hi there. This looks like a handy ability, however I'm worried that the web browser will download all references for a page like images, and run scripts, which is quite overkill for using this just to grab an HtmlDocument.
Any thoughts on how to avoid this?
By Anonymous on
Wednesday, March 05, 2008 10:08 PM
|
Re: Convert String to HTMLDocument in C# Without JavaScript Errors
There is a method that you can use to just get the pure HTML without anything else. I believe it's in the System.Net namespace. I'm drawing a blank at the moment though. Please allow me to blame the last 7 or so drinks for that. :)
In any event, you can do it. But the WebBrowser control is the wrong way for that.
Cheers,
Ryan
By dccath911 on
Wednesday, March 19, 2008 12:05 AM
|
Re: Convert String to HTMLDocument in C# Without JavaScript Errors
Thanks, you have saved my day! Manuel
By Anonymous on
Thursday, May 08, 2008 12:27 AM
|
Re: Convert String to HTMLDocument in C# Without JavaScript Errors
You can use the WebBrowser just to create the HTMLDocument(why, oh why didn't Microsoft make it a stand-alone class?).
Then use WebClient.DownloadString or DownloadStringAsync to get your raw string and write that to your freshly minted HTMLDocument.
-John
By Anonymous on
Tuesday, May 20, 2008 11:53 AM
|
|
|
|
 |
|
|
The Renegade Blog
|
 |
|
|
|
|
|
|
Tweets
|
 |
|
|
|
|
 |