Search code examples
htmlhtml-tablehtml-agility-pack

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


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.


Solution

  • 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