Why don't you just select the td
s directly?
foreach (HtmlNode col in doc.DocumentNode.SelectNodes("//table[@id='table2']//tr//td"))
Response.Write(col.InnerText);
Alternately, if you really need the tr
s separately for some other processing, drop the //
and do:
foreach (HtmlNode row in doc.DocumentNode.SelectNodes("//table[@id='table2']//tr"))
foreach (HtmlNode col in row.SelectNodes("td"))
Response.Write(col.InnerText);
Of course that will only work if the td
s are direct children of the tr
s but they should be, right?
EDIT:
var cols = doc.DocumentNode.SelectNodes("//table[@id='table2']//tr//td");
for (int ii = 0; ii < cols.Count; ii=ii+2)
{
string name = cols[ii].InnerText.Trim();
int age = int.Parse(cols[ii+1].InnerText.Split(' ')[1]);
}
There's probably a more impressive way to do this with LINQ.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…