Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# HTML
I'm trying to get all the tables from a website. Then go through all the
<td></td>
nodes.
for some reason the foreach loop gets the same dates from every single loop it goes through.
 
the HTML looks something like this.
<table>
 <tr> 
<td class=t>1.10</td><td class=t>2.10</td><td class=t>3.10</td><td class=t>4.10</td><td class=t>5.10</td><td class=t>6.10</td>
</tr>
</table>
 

HtmlNodeCollection weeks = doc.DocumentNode.SelectNodes("//table");
            foreach(HtmlNode week in weeks)
            {
                HtmlNodeCollection tempdate = week.SelectNodes("//td[@class='t']");
                for (int i = 0; i < 6; i++)
                {
                    dates[i] = tempdate[i].InnerText;
                }                
            }
Posted 8-Oct-12 6:26am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Your logic looks off.
HtmlNodeCollection weeks = doc.DocumentNode.SelectNodes("//table");
            foreach(HtmlNode week in weeks)
            {
                HtmlNodeCollection tempdate = week.SelectNodes("//td[@class='t']");
                for (int i = 0; i < 6; i++)
                {
                    dates[i] = tempdate[i].InnerText;
                }                
            }
 
In your code, every inner loop you loop through is just resetting the dates[] values when it loops through another HTMLNode (outer loop).
 
What do you intend to do with dates[]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 436
1 Maciej Los 249
2 BillWoodruff 199
3 /\jmot 180
4 Suraj Sahoo | Coding Passion 170
0 OriginalGriff 8,484
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,159
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100