Saturday, 15 March 2014

c# - Html Agility Pack loop through table rows and columns -



c# - Html Agility Pack loop through table rows and columns -

i have table this

class="lang-html prettyprint-override"><table border="0" cellpadding="0" cellspacing="0" id="table2"> <tr> <th>name </th> <th>age </th> </tr> <tr> <td>mario </td> <th>age: 78 </td> </tr> <tr> <td>jane </td> <td>age: 67 </td> </tr> <tr> <td>james </td> <th>age: 92 </td> </tr> </table>

and want utilize html agility pack parse it. have tried code no avail:

foreach (htmlnode row in doc.documentnode.selectnodes("//table[@id='table2']//tr")) { foreach (htmlnode col in row.selectnodes("//td")) { response.write(col.innertext); } }

what doing wrong?

i've run code , displays names, correct, because ages defined using invalid html: <th></td> (probably typo).

by way, code can simplified 1 loop:

foreach (var cell in doc.documentnode.selectnodes("//table[@id='table2']/tr/td")) { response.write(cell.innertext); }

here's code used test: http://pastebin.com/euzhuaah

c# .net html-agility-pack

No comments:

Post a Comment