Loading HTMLDocument from a string

  • Thread starter Thread starter Frank Perugini
  • Start date Start date
F

Frank Perugini

Hello All,

I have an HTML document stored in a database field and wish to load
and manipulate it with inline C# code like the following:

WebBrowser webControl = new WebBrowser();
webControl.DocumentText = htmlstringfromdb;
//Need to wait for DocumentCompleted event to be sure page is loaded.
HtmlDocument doc = webControl.Document;
string test = doc.Body.ToString();

The problem is that as indicated in the comment line above, it appears
if you try to set the DocumentText property of the WebBrowser control
with HTML, you need to wait for the DocumentCompleted event to be sure
it's loaded before trying to access the elements.

Does anyone know any way to wait for the document to load inline?

-Frank
 
Hello All,

I have an HTML document stored in a database field and wish to load
and manipulate it with inline C# code like the following:

WebBrowser webControl = new WebBrowser();
webControl.DocumentText = htmlstringfromdb;
//Need to wait for DocumentCompleted event to be sure page is loaded.
HtmlDocument doc = webControl.Document;
string test = doc.Body.ToString();

The problem is that as indicated in the comment line above, it appears
if you try to set the DocumentText property of the WebBrowser control
with HTML, you need to wait for the DocumentCompleted event to be sure
it's loaded before trying to access the elements.

Does anyone know any way to wait for the document to load inline?

-Frank

I found it myself. For others interested:

WebBrowser webControl = new WebBrowser();
webControl.DocumentText = htmlstringfromdb;

do
{
Application.DoEvents();
} while (webControl.ReadyState != WebBrowserReadyState.Complete);

string test = webControl.Document.Body.InnerHtml;

-Frank
 
Back
Top