Loading HTMLDocument from a string

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
 
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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top