html parse with HtmlAgilityPack in C#

asp.net c# html html-agility-pack parsing

Question

WebClient webClient = new WebClient();
string page = webClient.DownloadString(
    "http://www.deu.edu.tr/DEUWeb/Guncel/v2_index_cron.html");

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(page);

I want to parse the page that is given above but I want to get table's row information. I've tried to do with several examples but I could not manage to do that. Any suggestion

Accepted Answer

You could for example parse the rows like this:

using System.Net;
using HtmlAgilityPack;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            WebClient webClient = new WebClient();
            string page = webClient.DownloadString("http://www.deu.edu.tr/DEUWeb/Guncel/v2_index_cron.html");

            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(page);

            HtmlNode table = doc.DocumentNode.SelectSingleNode("//table");
            foreach (var cell in table.SelectNodes("tr/td"))
            {
                string someVariable = cell.InnerText;
            }
        }
    }
}

For completeness, using LINQ you can easily create an enumerable that contains all non-empty row values:

    private static void Main(string[] args)
    {
        WebClient webClient = new WebClient();
        string page = webClient.DownloadString("http://www.deu.edu.tr/DEUWeb/Guncel/v2_index_cron.html");

        HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml(page);

        HtmlNode table = doc.DocumentNode.SelectSingleNode("//table");
        var rows = table.SelectNodes("tr/td").Select(cell => cell.InnerText).Where(someVariable => !String.IsNullOrWhiteSpace(someVariable)).ToList();
    }

Popular Answer

Here's an example of enumerating through all of the table cells and writing out each ones inner text to the console

WebClient webClient = new WebClient();
var page = webClient.DownloadString("http://www.deu.edu.tr/DEUWeb/Guncel/v2_index_cron.html");

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(page);

foreach (var td in doc.DocumentNode.SelectNodes("//table/tr/td"))
{
    Console.WriteLine(td.InnerText);
}


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why