loop over HtmlNodeCollection

Topics: Developer Forum
Mar 13, 2009 at 3:06 PM
Edited Mar 13, 2009 at 3:11 PM

First post here so bear with me.

I'm using the HTMLAgilityPack to scrape some data which I then want to place in a DataTable. The data i'm scraping comes in the form of a table with 3 columns. I want each row in the Datatable to correspond to each row in the HTML table. So here's where i've got so far and where i'm coming unstuck:

This code actually inserts the info I need into Col1 but then inserts the last record in the HTMLNodeCollection for Col2 and Col3 into all the rows.

Can anybody point me in the right direction with this???

Thanks in advance


DataTable ssDT = new DataTable();


"Col1", typeof(String));
"Col2", typeof(String));
"Col3", typeof(String));



HtmlWeb web = new HtmlWeb();  


HtmlDocument doc = web.Load("url");




HtmlNodeCollection col1Options = doc.DocumentNode.SelectNodes("//td[@class=\"col1\"]");
HtmlNodeCollection col2Options = doc.DocumentNode.SelectNodes("//td[@class=\"col2\"]");
HtmlNodeCollection col3Options = doc.DocumentNode.SelectNodes("//td[@class=\"col3\"]");




foreach (HtmlNode col1Option in col2Options) 
    DataRow row = ssDT.NewRow();
"Col1"] = col1Option.InnerText;



for (int i = 0; i < col1Options.Count; i++)
    foreach (HtmlNode col2Option in col2Options) 
"Col2"] = col2Option.InnerText;
oreach (HtmlNode col3option in col3options) 
"Col3"] = col3Option.InnerText; 

GridView1.DataSource = ssDT;