HtmlAgilityPack cannot find specific td

c# html-agility-pack xpath

Question

I need extract value of just one specific td from the table by using XPath, but code always return null. How can I fix this?

var location = GetLocation(document.Result.DocumentNode.SelectSingleNode("//*[@id='detailTabTable']/tbody/tr[3]/td[2]"));

and the code

private string GetLocation(HtmlNode h)
        {
            try
            {
                string location = null;
                if (h == null)
                {
                    location = "N/A";
                }
                else
                {
                    location = h.InnerText;
                    location = location.Substring(0, location.IndexOf(",", StringComparison.InvariantCulture));
                }
                return location;
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Error in Link Data Repository {0} in Parse Links {1}", ex.Message, ex.StackTrace);
                throw new Exception(ex.Message);
            }
        }

And small simple table:

       <table id="detailTabTable" width="99%" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td class="detailTabContentLt">Current List Price:</td>
                <td class="detailTabContentPriceRt">
                  <span class="aiDetailCurrentPrice">AED 6,600,000</span>
                </td>
            </tr>
            <tr>
                <td class="detailTabContentLt" style="white-space: nowrap;">Plot size (Sq. Ft.):</td>
                <td class="detailTabContentRt">N/A</td>
            </tr>
            <tr>
                <td class="detailTabContentLt" valign="top">Locality</td>
                <td class="detailTabContentRt">Dubai, Dubai</td>
            </tr>
            <tr>
                <td colspan="2"></td>
            </tr>
        </table>

Popular Answer

I have just tested your code. As mentioned in the comments when you do remove tbody from your xpath expression everything works fine. This worked fine for me.

private static void htmlAgilityPackTest()
{
    string html = " <table id=\"detailTabTable\" width=\"99%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td class=\"detailTabContentLt\">Current List Price:</td><td class=\"detailTabContentPriceRt\"><span class=\"aiDetailCurrentPrice\">AED 6,600,000</span></td> </tr><tr> <td class=\"detailTabContentLt\" style=\"white-space: nowrap;\">Plot size (Sq. Ft.):</td><td class=\"detailTabContentRt\">N/A</td></tr> <tr><td class=\"detailTabContentLt\" valign=\"top\">Locality</td> <td class=\"detailTabContentRt\">Dubai, Dubai</td> </tr> <tr><td colspan=\"2\"></td> </tr>  </table>";
    HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
    document.LoadHtml(html);

    var node = document.DocumentNode.SelectSingleNode("//*[@id='detailTabTable']/tr[3]/td[2]");
    string location = GetLocation(node);
    Console.WriteLine("Location: " + location);
}

In case I misunderstood anything please let me know.



Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow