Skip to content Skip to sidebar Skip to footer

Html Agility Pack Parse Table

I have a table like this:
Name Age

Solution 1:

You can grab the cell content from within your outer foreach loop:

foreach (HtmlNode td in doc.DocumentNode.SelectNodes("//table[@id='table2']//tr//td"))  
{  
    Response.Write(td.InnerText);   
}  

Also I'd recommend trimming and 'de-entitizing the inner text to ensure it is clean:

Response.Write(HtmlEntity.DeEntitize(td.InnerText).Trim())

In your source the cells for [Age: 78] and [Age: 92] have a <th> tag at the start instead of <td>

Solution 2:

This is my solution. Please notice your HTML is not well formatted because you have TH where TD should be:

<tableborder="0"cellpadding="0"cellspacing="0"id="table2"><tr><th>Name
        </th><th>Age
        </th></tr><tr><td>Mario
        </td><td>Age: 78
        </td></tr><tr><td>Jane
        </td><td>Age: 67
        </td></tr><tr><td>James
        </td><td>Age: 92
        </td></tr></table>

And this is the c# Code:

using HtmlAgilityPack;

namespaceConsoleApplication1
{
    classProgram
    {
        staticvoidMain(string[] args)
        {

            HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
            document.Load("page.html");

            List<HtmlNode> x = document.GetElementbyId("table2").Elements("tr").ToList();

            foreach (HtmlNode node in x)
            {
                List<HtmlNode> s = node.Elements("td").ToList();
                foreach (HtmlNode item in s)
                {
                    Console.WriteLine("TD Value: " + item.InnerText);
                }
            }
            Console.ReadLine();
        }
    }
}

Screenshot: enter image description here

Edit: I must add that if you are going to use the <th> tags you must include them inside a <thead> tag, and then your rows inside of a <tbody> tag so that your html is well formatted :)

More info: http://www.w3schools.com/tags/tag_thead.asp

Post a Comment for "Html Agility Pack Parse Table"