Wednesday, May 4, 2016

Parse html table using HTML Agility Pack

Below is a simple code to parse a table using HTML Agility Pack. Make sure to reference the HTML Agility Pack namespace in your program.
1
using HtmlAgilityPack;

Parse Table
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load("http://your_sample_url");
 
// Get all tables in the document
HtmlNodeCollection tables = doc.DocumentNode.SelectNodes("//table");
 
// Iterate all rows in the first table
HtmlNodeCollection rows = tables[0].SelectNodes("tr");
for (int i = 0; i <= rows.Count - 1; i++)
{
    // Iterate all columns in this row
    HtmlNodeCollection cols = rows[i].SelectNodes("td");
    if (cols != null)
    {
        for (int j = 0; j <= cols.Count - 1; j++)
        {
            // Get the value of the column and print it
            string value = cols[j].InnerText;
            Console.WriteLine(value);
        }
    }
}

0 comments:

Post a Comment