The HTML parser allows you to parse HTML and return a HTMLDocument. Html Parser
Name |
Description |
From File |
Loads an HTML document from a file. |
From String |
Loads the HTML document from the specified string. |
From Web |
Gets an HTML document from Internet resource. |
From Browser |
Gets an HTML document from a WebBrowser. |
loading HTML from a file
Htmldocument.load method to load an HTML document from a file
The following example loads HTML from a file
var path = @ "test.html";
var doc = new HTMLDocument ();
Doc. Load (path);
var node = doc. Documentnode.selectsinglenode ("//body");
Console.WriteLine (node. outerHTML);
loading HTML from a string
The Htmldocument.loadhtml method loads the HTML document from the specified string.
The following example loads HTML from a specified string.
var html = @ "<! DOCTYPE html>
load HTML from the Web
The Htmlweb.load method gets an HTML document from an Internet resource.
The following example loads HTML from the Web
var html = @ "http://html-agility-pack.net/";
Htmlweb Web = new Htmlweb ();
var htmldoc = web. Load (HTML);
var node = htmlDoc.DocumentNode.SelectSingleNode ("//head/title");
Console.WriteLine ("Node Name:" + node.) Name + "\ n" + node. outerHTML);
loading HTML from WebBrowser
The Htmlweb.load method obtains an HTML document from a Web browser. You can wait for JavaScript to run by customizing the isbrowserscriptcompleted parameter.
The following example loads HTML from WebBrowser and waits until the text is set to Div.
string url = "Http://html-agility-pack/from-browser";
var web1 = new Htmlweb (); var Doc1 = Web1.
Loadfrombrowser (URL, o => {var webBrowser = (webBrowser) o; Wait until the dynamic text is set to return!string. IsNullOrEmpty (WebBrowser.Document.GetElementById ("Uidynamictext").
InnerText);
}); var T1 = Doc1. Documentnode.selectsinglenode ("//div[@id = ' uidynamictext ']").
InnerText;
var web2 = new Htmlweb (); var doc2 = web2. Loadfrombrowser (URL, HTML => {//wait until dynamic text is set to return!html.
Contains ("<div id=\" uidynamictext\ "></div>");
}); var t2 = doc2. Documentnode.selectsinglenode ("//div[@id = ' uidynamictext ']").
InnerText;
Console.WriteLine ("Text 1:" + T1); Console.WriteLine ("Text 2:" + T2);