Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

When there is a compound table inside an html file how can one count the rows of the parent table.

What I mean by a compound table; a table in which other tables are contained within some of its cells.

Here is my attempt at coding. Note I receive an incorrect values:

        String htmlFile = "C:/Temp/Test_13.html";
        HtmlDocument doc = new HtmlDocument();
        doc.Load(htmlFile);

        HtmlNodeCollection tables = doc.DocumentNode.SelectNodes("//table");
        HtmlNodeCollection rows = tables[1].SelectNodes(".//tr");
        Console.WriteLine(" Rows in second (Parent) table: " + rows.Count());

Please indicate which namespace is used in your answer.

Here is a representative sample file:

<html>
<body>
<table border="1">
<tr>
<td>Apps</td>
</tr>
<tr>
<td>Offcie Web Apps</td>
</tr>
</table>
<br/>
<table border="1">
<tr>
<td>Application</td>
<td>Status</td>
<td>Instances</td>
</tr>
<tr>
<td>PowerPoint</td>
<td>Online</td>
<td>
    <table border="1">
    <tr>
        <td>Server1</td>
        <td>Online</td>
    </tr>
    <tr>
        <td>Server2</td>
        <td>Disabled</td>
    </tr>
    </table>
</td>
</tr>
<tr>
<td>Word</td>
<td>Online</td>
<td>
    <table border="1">
    <tr>
        <td>Server1</td>
        <td>Online</td>
    </tr>
    <tr>
        <td>Server2</td>
        <td>Disabled</td>
    </tr>
    </table>
</td>
</tr>
</table>
</body>
</html>

Thank you.

share|improve this question
You're using Visual Studio, right? Examine your tables and rows collections by hovering over them during a debug; see what's in there. – Robert Harvey 18 hours ago

2 Answers

You can push each <table> and <tr> to the stack and when you encounter </table> - pop until the table is popped from the stack.

share|improve this answer
It's helpful to have more specific detail. What do you mean by the "stack?" Do you mean a data structure that they would instantiate and parse the HTML, or something else? It doesn't look like they're already using that approach. Also, please use <> around DOM elements, that makes them easier to read, as well as using code formatting around them (and any other code snippets you use). Thanks! – bcr 18 hours ago

I would recommend you try the csQuery nuget package. It's designed to take most of the headaches out of doing things exactly like that. You can use the css selector query syntax, which most web devs are quite familiar with. In this case, you could probably get away with body > table:nth-of-type(2) > tr and it will return an array of all the tr's, then just count them, or check the length of the resulting array. Alternatively, body > table ~ table > tr would work as well from the sample you gave as would br + table > tr

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.