Using HtmlAgilityPack to get the last <tr> of a html table

html html-agility-pack html-table

Question

I have a html table stucture. I need to get the value of the value of the first <td> in the final <tr> tag. Here is my table structure. The value I require from the below function getFinalNodeValue is "3".

 <table id="test">
            <tr>
                <td>ID</td>
                <td>Name</td>
                <td>Age</td>
            </tr>
            <tr>
                <td>1</td>
                <td>Yasoda</td>
                <td>21</td>
            </tr>

            <tr>
                <td>2</td>
                <td>Samantha</td>
                <td>25</td>
            </tr>

            <tr>
                <td>3</td>
                <td>Sajee</td>
                <td>26</td>
            </tr>

        </table>


Here is the code I wrote using HtmlAgilityPack.

 public String getFinalNodeValue(String URL)
        {
            var webGet = new HtmlWeb();
            var pageSource = webGet.Load(URL);

            var table = pageSource.DocumentNode.SelectSingleNode("//table[@id='test']//tr[1]");


            string id = null;


            IEnumerable<HtmlNode> trNodes = table.DescendantsAndSelf();

            foreach (var currentItem in trNodes)
            {
                if (currentItem == trNodes.Last())
                {
                    IEnumerable<HtmlNode> tdNodes = currentItem.Descendants();

                    foreach (var x in tdNodes)
                    {
                        if(x == tdNodes.First())
            {
                id = x.InnerText;
            }
            else
            {
                break;
            }
                    }

                }
                else
                {
                    continue;
                }
            }

            return id;

        }

The method doesn't return a value. Any help is highly appreciated.

Accepted Answer

This should do it:

    HtmlDocument doc = new HtmlDocument();
    doc.Load(MyHtmlFile);

    HtmlNode node = doc.DocumentNode.SelectSingleNode("//table[@id='test']/tr[last()]/td");
    Console.WriteLine(node.InnerText);

Note the usage of the XPATH last() function


Popular Answer

The XPath you use to populate the table variable - "//table[@id='test']//tr[1]", selects the second TR element, no the table.

This most likely should just be "//table[@id='test']".

At this point, to fetch the descendant TR nodes into the trNodes variable, you should use:

IEnumerable<HtmlNode> trNodes = table.SelectNodes("tr");



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