473,473 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

HtmlElementCollection foreach loop won't end

1 New Member
Hello, First time posting and new to C# so please forgive my noobishness. I have built a little page scraper to grab a list of television shows from and input string. Here is the class:

Expand|Select|Wrap|Line Numbers
  1.     public class Show
  2.     {
  3.         public ArrayList seasons;
  4.         public string name;
  5.         public Boolean loaded;
  6.         public int showId;
  7.         public Show(string n)
  8.         {
  9.             name = n;
  10.             WebBrowser scraper = new WebBrowser();
  11.             scraper.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(this.scrapeForShows);
  12.             scraper.Url = new Uri("http://www.tv.com/search.php?type=Search&stype=ajax_search&search_type=program&offset=0&qs=" + name);
  13.         }
  14.         public void scrapeForShows(object sender, WebBrowserDocumentCompletedEventArgs e)
  15.         {
  16.             HtmlDocument doc = ((WebBrowser)sender).Document;
  17.             HtmlElementCollection spans = doc.GetElementsByTagName("span");
  18.             Dictionary<string, string> showsFound = new Dictionary<string, string>();
  19.             for (int n = 0; n < spans.Count;n++)
  20.             {
  21.                 var html = spans[n].InnerHtml.ToString();
  22.                 if (Regex.IsMatch(html, @"Show\:"))
  23.                 {
  24.                     string showName = Regex.Match(html, @"<(a|A).*>(?<show>.*)</(a|A)>").Groups["show"].ToString();
  25.                     showName = Regex.Match(showName, @"\w.*\w").ToString();
  26.                     showsFound.Add(showName, Regex.Match(html, @"/show/(?<num>\d+)/").Groups["num"].ToString());
  27.                 }
  28.             }
  29.             if (showsFound.ContainsKey(name))
  30.             {
  31.                 MessageBox.Show(name.ToString());
  32.             }
  33.         }
  34.     }
I works fairly well but the for statement never ends. And even stranger still the document complete fires 3 times. When I debug it says that the spans.Count is 16 and it increments 1,2 .. 14 then stops. It doesn't hang, it doesn't do 15, it just ends and doesn't execute the code after.

The last span on the scrapped page is empty <span></span>. Is that the source of my problem? Why would that loop just stop processing.

PS I switched to the iterative for statement after I originally had the same problem with a foreach and wanted to see what was happening.

Any help would be GREATLY appreciated. I'm really stumped. Also any advise you have as far as technique would be really helpful.
Mar 21 '08 #1
1 4422
nateraaaa
663 Recognized Expert Contributor
Try setting n = 1 and n <= spans.Count. Let me know if you get a different result.

Nathan
Mar 24 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Randell D. | last post by:
Folks, Ever since reading an interesting article in Linux Format on PHP whereby suggested code writing was made that could enhance performance on a server, I've started testing various bits of...
7
by: Phil | last post by:
Hi, I read somewhere that the new version (v1.1) has improved the performance of 'foreach' over 'for'. Is that true? I did some measurements and I still think for has an upperhand... ? Phil
104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
13
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in...
7
by: david | last post by:
I need 5 queries from the database, which I display in a php foreach loop the following way. $query1 = $DB->query("SELECT ... DESC LIMIT 20"); $query2 = $DB->query("SELECT ... DESC LIMIT 20");...
3
by: Akira | last post by:
I noticed that using foreach is much slower than using for-loop, so I want to change our current code from foreach to for-loop. But I can't figure out how. Could someone help me please? Current...
9
by: Nathan Sokalski | last post by:
I am trying to use the System.Array.ForEach method in VB.NET. The action that I want to perform on each of the Array values is: Private Function AddQuotes(ByVal value As String) As String Return...
7
by: Osiris | last post by:
Just something I would like to share: I just learned the hard way (2 days detective work on a bug) that foreach loops are not at all like for loops, not intuitive at all. BEWARE: arrays and...
23
by: tshad | last post by:
Is there a way to know if you are looking at the last record record of foreach loop other then setting up a loop counter that you manually increment? foreach (Racecar racecar in...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.