Click here to Skip to main content
12,064,656 members (26,466 online)
Rate this:
 
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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160204.4 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2016
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