html - get all td in XPath c# -
i trying parse html using htmlagilitypack in c#. have 21 tr items
, each tr items have 7 td items
. how can tr , td items in order? can 1 tr item , 7 td items.
here c# code:
var url = "url"; httpwebrequest request = (httpwebrequest)webrequest.create(url); httpwebresponse response = (httpwebresponse)request.getresponse(); streamreader sr = new streamreader(response.getresponsestream()); string sourcecode = sr.readtoend(); htmlagilitypack.htmldocument document = new htmlagilitypack.htmldocument(); document.loadhtml(sourcecode); var name = document.documentnode.selectnodes("//*[@id=\"searchresultstable\"]/tbody/tr[1]/td[2]/a[1]")[0].innertext; var year = document.documentnode.selectnodes("//*[@id=\"searchresultstable\"]/tbody/tr[1]/td[3]")[0].innertext; var km = document.documentnode.selectnodes("//*[@id=\"searchresultstable\"]/tbody/tr[1]/td[4]")[0].innertext; var color = document.documentnode.selectnodes("//*[@id=\"searchresultstable\"]/tbody/tr[1]/td[5]")[0].innertext; var price = document.documentnode.selectnodes("//*[@id=\"searchresultstable\"]/tbody/tr[1]/td[6]")[0].innertext; var date = document.documentnode.selectnodes("//*[@id=\"searchresultstable\"]/tbody/tr[1]/td[7]")[0].innertext; var location = document.documentnode.selectnodes("//*[@id=\"searchresultstable\"]/tbody/tr[1]/td[8]")[0].innertext;
i tried use [@id=\"searchresultstable\"]/tbody/tr[1]/td[position()<8]
returns /n
try below code (not tested. chances of compile errors. gives idea.)
comments in code gives more details.
//get table node htmlnode table = document.documentnode.selectsinglenode("//*[@id='searchresultstable']"); //loop through table node , find each tr foreach (htmlnode row in table.selectnodes("//tr")) { //print here whatever want each row. console.writeline("new row"); //loop through td of each tr foreach (htmlnode cell in row.selectnodes("//td")) { //print here each td console.writeline("cell: " + cell.innertext); } //end td }//end tr
Comments
Post a Comment