CSharp · Console
using System;
using HtmlAgilityPack;
using System.Linq;
public class Program
{
// description: Showing here how to parse complex web HTML table using HTML Agility Pack C#
public static void Main()
{
// declare object of HtmlDocument
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(@"<table id=""TC""><tr><th>Name</th></tr><tr><td>Technology</td></tr><tr><td>Crowds</td></tr></table>");
// Using LINQ to parse HTML table smartly
var HTMLTableTRList = from table in doc.DocumentNode.SelectNodes("//table").Cast<HtmlNode>()
from row in table.SelectNodes("tr").Cast<HtmlNode>()
from cell in row.SelectNodes("th|td").Cast<HtmlNode>()
select new {Table_Name = table.Id, Cell_Text = cell.InnerText};
// now showing output of parsed HTML table
foreach(var cell in HTMLTableTRList)
{
Console.WriteLine("{0}: {1}", cell.Table_Name, cell.Cell_Text);
}
}
}