Awais Shah
Awais Shah

Reputation: 31

How to Retrieve Specific table from HTML FILE in c#?

I have an HTML file that contains many tables, but I want to access a specific table from the file (not all tables). So how can I do that? Code is look something like below and all tables are without ids

`<table border=1>
<tr><td>VI not loadable</td><td>0</td></tr>
<tr><td>Test not loadable</td><td>0</td></tr>
<tr><td>Test not runnable</td><td>0</td></tr>
<tr><td>Test error out</td><td>0</td></tr>
</table>`

Upvotes: 0

Views: 638

Answers (1)

Farshad Nasehi
Farshad Nasehi

Reputation: 181

every table should have an Id or something that could be Identified from the others, if so you can get it via jquery. for example :

 <table class="table table-striped" id="tbl1">
<thead>
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Email</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>John</td>
    <td>Doe</td>
    <td>[email protected]</td>
  </tr>
  <tr>
    <td>Mary</td>
    <td>Moe</td>
    <td>[email protected]</td>
  </tr>
  <tr>
    <td>July</td>
    <td>Dooley</td>
    <td>[email protected]</td>
  </tr>
</tbody>

and get it like this:

var table = $('#tbl1').html();

if not you can find it by its priority in the file. for example you can access to 2nd table like this :

var table = $('table:nth-child(2)')

or in C# maybe this would help:

HtmlNode table = doc.DocumentNode.SelectSingleNode("//table[1]")
foreach (var cell in table.SelectNodes(".//tr/td")) 
{
     string someVariable = cell.InnerText
}

Upvotes: 1

Related Questions