Donate

Parse Html Table Using HTML Agility Pack In C#

Below is a simple code to parse a table using HTML Agility Pack. Make sure to add the Html Agility Pack package from Nuget and reference that library in the namespace of 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);
        }
    }
}

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

Bootstrap Modal In ASP.NET MVC With CRUD Operations