Get HtmlDocument from string in C#

Hi,

In this article I want to share how we can convert a string containing HTML content to HtmlDocument (System.Windows.Forms.HtmlDocument).

Some of you might thing why I want to do that but the answer is simply my need, actually HtmlDocument class profiles many methods to manipulate HTML content in any string. For example it gives various javascript like operations to reach and operate on HTML elements.

I searched for it many times but didn’t find any single place where I get exact solution, so after some hit and tries and combining solutions given n various Stackoverflow questions I come up with this simple code block.

        public System.Windows.Forms.HtmlDocument GetHtmlDocument(string html)
        {
            WebBrowser browser = new WebBrowser();
            browser.ScriptErrorsSuppressed = true; //not necessesory you can remove it
            browser.DocumentText = html;
            browser.Document.OpenNew(true);
            browser.Document.Write(html);
            browser.Refresh();
            return browser.Document;
        }

I used to set ScriptErrorsSuppressed to true ever time I use WebBrowser control because this get me out of any script error or warning.

Leave a Reply