February 14, 2016

Use HtmlAgilityPack to parse HTML tag

We can use HtmlAgilityPack to parse HTML files.
It is very easy to use.

Let's see an example here.
Here I want to access p tag from url's view source.

var html = new HtmlDocument();
html.LoadHtml(
new WebClient().DownloadString("Your URL"
));
var
root = html.DocumentNode;
var node = html.DocumentNode.SelectNodes("//p[contains(@class, 'show')]");
// Get p tag which contains class show
var inputControls = htmlNode.Descendants("input"); //To get input descendants from node
var selectControls = htmlNode.Descendants("select"); //To get select descendants from node

No comments:

Post a Comment