How to get count of parent row nodes from a html using HtmlAgilityPack

c# html-agility-pack

Question

Below is my html page:

Using Html Agility Pack, i need to get the Main Table Main Rows. Means in the below page, Only One Main Table and Only 3 Main Rows Under the Main table.

I need to get the count of 3, excluding all inner TR.

Please help.

HTML :

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
        <title>Untitled Page</title>

    </head>
    <body style="width: 800">
        <table  width="700" style="background-repeat: no-repeat;">
            <tr>
                <td>
                    <table width="700">
                        <tr>
                            <td width="20%"></td>
                            <td width="60%" align="center" style="font-family: Arial; font-size: 12pt;"> SUMMARY </td>
                            <td width="20%"></td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td>
                    <table width="700">
                        <tr>
                            <td width="20%"></td>
                            <td width="60%" align="center" style="font-family: Arial; font-size: 12pt;"> SUMMARY </td>
                            <td width="20%"></td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td>
                    <table width="700">
                        <tr>
                            <td width="20%"></td>
                            <td width="60%" align="center" style="font-family: Arial; font-size: 12pt;"> SUMMARY </td>
                            <td width="20%"></td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </body>
    </html>

code :

  var doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(htmlFile);


          int count = 1;
          HtmlNodeCollection tables = doc.DocumentNode.SelectNodes("//table");
          foreach (HtmlNode table in tables)
          {

              var rows = doc.DocumentNode.SelectNodes("//tr");
              //here i need to get those 3 rows only.
              //but instead i am getting all inner TR also.

              foreach (HtmlNode tr in rows)
              {

              }
          }

Accepted Answer

You are selecting all the tables.

Try selecting just the table that is a child of body, something like:

SelectSingleNode("//body/table");

Then, count the TRs in that node.

Or, try doing it all in one step:

SelectNodes("//body/table/tr");


Related

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