Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a string or html file to C# HtmlDocument without using WebBrowser or HAP

Tags:

browser

dom

c#

The only solution I could find was using:

            mshtml.HTMLDocument htmldocu = new mshtml.HTMLDocument();
            htmldocu .createDocumentFromUrl(url, "");

and I am not sure about the performance, it should be better than loading the html file in a WebBrowser and then grab the HtmlDocument from there. Anyhow, that code does not work on my machine. The application crashes when it tries to execute the second line.

Has anyone an approach to achieve this efficiently or any other way?

NOTE: Please understand that I need the HtmlDocument object for DOM processing. I do not need the html string.

like image 750
Devela Avatar asked Mar 21 '26 07:03

Devela


1 Answers

Use the DownloadString method of the WebClient object. e.g.

WebClient client = new WebClient();
string reply = client.DownloadString("http://www.google.com");

In the above example, after executed, reply will contain the html markup of the endpoint http://www.google.com.

WebClient.DownloadString MSDN

like image 189
George Johnston Avatar answered Mar 22 '26 19:03

George Johnston



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!