c# - htmlAgilityPack parse table to datatable or array -


i have these tables:

<table> <tbody> <tr><th>header 1</th></tr> </tbody> </table>  <table> <tbody> <tr> <th>header 1</th> <th>header 2</th> <th>header 3</th> <th>header 4</th> <th>header 5</th> </tr> <tr> <td>text 1</td> <td>text 2</td> <td>text 3</td> <td>text 4</td> <td>text 5</td> </tr> </tbody> </table> 

i trying transform array or list using code:

var query = table in doc.documentnode.selectnodes("//table").cast<htmlnode>()                          row in table.selectnodes("tr").cast<htmlnode>()                          header in row.selectnodes("th").cast<htmlnode>()                          cell in row.selectnodes("td").cast<htmlnode>()                          select new {                               table = table.id,                               row = row.innertext,                               header = header.innertext,                              celltext = cell.innertext                          }; 

but doesn't work. wrong?

some notes:

  • you not need cast
  • you assuming each row have headers
  • selectnodes needs recieve xpath , passing names

if use foreach , model data, way have more control , efficiency, if still want way how should be

var query = table in doc.documentnode.selectnodes("//table")                         table.descendants("tr").count() > 1 //make sure there rows other header row                         row in table.selectnodes((".//tr[position()>1]")) //skip header row                         cell in row.selectnodes(("./td"))                          header in table.selectnodes(".//tr[1]/th") //select header row cells first tr                         select new                         {                             table = table.id,                             row = row.innertext,                             header = header.innertext,                             celltext = cell.innertext                         }; 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -