I had hard time to find how to convert plain string HTML source to HtmlDocument (.net object).

As HtmlDocument does not have “new” constructor so we can’t create HtmlDocument object on the fly.

I have created following function for this purpose. Hope it helps!!

VB.net

Public Function Html2Doc(ByVal src As String) As HtmlDocument
        Dim w As WebBrowser = New WebBrowser
        w.DocumentText = src
        Do
            Application.DoEvents()
        Loop While w.ReadyState <> WebBrowserReadyState.Complete
        Return w.Document
    End Function

C#

public HtmlDocument Html2Doc(string src)
{
	WebBrowser w = new WebBrowser();
	w.DocumentText = src;
	do
	{
		Application.DoEvents();
	} while (w.ReadyState != WebBrowserReadyState.Complete);
	return w.Document;
}

Leave a reply