cojac
cojac

Reputation: 85

How to get rows from two table in the same xpath expression?

I'm using HtmlAgilityPack. Suppose I have the following situation:

<div class="main container">
   <div class="left">
       <table>
           <tbody>
             <tr />
             <tr />
             <tr />
           </tbody>
      </table>
   </div>
   <div class="right">
       <table>
           <tbody>
             <tr />
             <tr />
             <tr />
           </tbody>
      </table>
   </div>

I want get all the rows contained in the tbody from the two table of left and right, contained in the main container div.

I tried:

HtmlNode main = doc.DocumentNode.SelectSingleNode("//div[@class='main container']");

HtmlNodeCollection rows = main.SelectNodes("//table//tr");

but this will return all the rows of the document, I need only the rows of the two table.

Upvotes: 1

Views: 63

Answers (1)

Jonathan Magnan
Jonathan Magnan

Reputation: 11347

Try this:

main.SelectNodes("/table//tr");

When starting the SelectNode with two slashes, it restarts the search from the root document instead of the current path. I believe that's your issue.

Upvotes: 1

Related Questions