Select Table Rows by Grouping them Adjacent to each Other using XPath

Posted by Adnan Yaseen on Stack Overflow See other posts from Stack Overflow or by Adnan Yaseen
Published on 2014-08-18T22:17:01Z Indexed on 2014/08/18 22:20 UTC
Read the original article Hit count: 323

Filed under:
|

I am not clear how to express my problem correctly in the question so forgive me if I am not able to properly convey my problem. I have following data.

<tr class="header">Random Value 1</tr>
<tr class="item">1</tr>
<tr class="item">2</tr>
<tr class="item">3</tr>
<tr class="header">Random Value 2</tr>
<tr class="item">4</tr>
<tr class="item">5</tr>
<tr class="item">6</tr>
<tr class="header">Random Value 3</tr>
<tr class="item">7</tr>
<tr class="item">8</tr>
<tr class="item">9</tr>

What I want to acheive is that I want to select the with class header. I have achieved this by using the following line of code,

HtmlNodeCollection headerNodes = doc.DocumentNode.SelectNodes("//tr[@class='header']");

Now I have all the header rows in the collection. Now I loop through all the header nodes and I want to get the table rows which are adjacent to the respective header rows.

foreach (HtmlNode node in headerNodes)
{
     HtmlNodeCollection itemNodes = ???
}

My question is that what I should write here so that for header row with text "Random Value 1" I get the item rows 1,2 and 3. Similarly for header row with text "Random Value 2" I get the item row 4,5 and 6 and so on.

© Stack Overflow or respective owner

Related posts about c#

Related posts about xpath