Sheil
Sheil

Reputation: 213

HtmlAgilityPack reading table after specified table

I have similiar structure to this:

<table class="superclass">                    
   <tr>                                                                  
       <td>    
       </td>                                                            
       <td>                                                     
       </td>                                                             
    </tr>                                                                
   <tr>                                                                  
       <td>                                                        
       </td>                                                             
       <td>                                              
       </td>                                                            
    </tr>                                                                
</table>                                                                

<table cellspacing="0">                                                  
    <tr>                                                                 
       <td>                                      
       </td>                                                            
       <td>
       </td>                                                            
    </tr>                                                    
    <tr>                                                                 
       <td>                                                       
       </td>                                                            
        <td>                                                       
       </td>                                                            
    </tr>                                                                
</table>                   

This is how I get the first table with class:

HtmlNode firstTable = document.DocumentNode.SelectSingleNode("//table[@class=\"superclass\"]");

Then I read the data. However I don't know how to get straight to the another table and read that data too. Any ideas?

I'd rather avoid counting which table it is and then using index to that table.

Upvotes: 0

Views: 91

Answers (2)

har07
har07

Reputation: 89315

There is XPath following-sibling axis which allows you to get element following current context element at the same level :

HtmlNode firstTable = document.DocumentNode.SelectSingleNode("//table[@class=\"superclass\"]");
HtmlNode nextTable = firstTable.SelectSingleNode("following-sibling::table");

Upvotes: 1

Ji_in_coding
Ji_in_coding

Reputation: 1701

If you want to access multiple nodes, you can consider SelectNodes(xpath) method over SelectSingleNode(xpath) method.

I'll provide a sample code here for reference, it may not work towards your need.

        var tables = htmlDocument.DocumentNode.SelectNodes("//table");
        foreach (HtmlNode table in tables)
        {
            if (table.GetAttributeValue("class", "").Contains("superclass"))
            {
                //this is the table of class="superclass"
            }
            else
            {
                //this is the other table.
            }
        }

Upvotes: 1

Related Questions