Anup Patil
Anup Patil

Reputation: 209

Get all table tr data in single HtmlNodeCollection c#

I need to retrieve all tr from all tables using HTML Agility Pack.

HTML:

<section class="content-section" id="more">
    <div class="row">
        <div class="col-xs-6"></div>
        <div class="col-xs-6">
            <h2>Specs</h2>
            <div>
                <div>
                    <table>
                        <thead>
                            <tr><th colspan="2"> test</th></tr>
                        </thead>
                        <tbody>                         
                            <tr><td>2</td><td>b</td></tr>
                            <tr><td>1</td><td>a</td></tr>
                        </tbody>
                    </table>                                    
                    <table>
                        <tbody>
                            <tr><td>3</td><td>c</td></tr>
                            <tr><td>4</td><td>d</td></tr>               
                        </tbody>
                    </table>
                </div>              
            </div>
        </div>      
    </div>  
</section>

C#:

HtmlNodeCollection featuresNode = document.DocumentNode.SelectNodes("//*[@id='more']/div/div[2]/div/div[1]/table/tbody/tr");

I am only able to get first table tr but not getting all two table tr in HtmlNodeCollection.

Upvotes: 1

Views: 1602

Answers (1)

krlzlx
krlzlx

Reputation: 5822

To get all tr nodes including the one in thead, update your XPath to:

"//*[@id='more']/div/div[2]/div/div[1]/table//tr"

This simplified XPath should also work:

"//*[@id='more']//tr"

If you want only the tr from tbody, use:

"//*[@id='more']//tbody//tr"

Or excluding tr from thead, use:

"//*[@id='more']//tr[not(ancestor::thead)]"

Upvotes: 3

Related Questions