c# - Getting a loop to build a string correctly -
i'm trying pull tournament age webpage:
http://www.reddishvulcans.com/uk_tournament_database.asp
i'm trying create string based on valid ages entry each table.
for example, if "carling cup" enterable 7 year olds, string generated "u7", or if it's enterable 7, 8, , 9 year olds, resulting string "u7, u8, u9".
i've made start, logic break if ages go "under 7s, gap here no under 8s, under 9s".
here code:
public static list<record> getrecords() { string url = "http://www.reddishvulcans.com/uk_tournament_database.asp"; var webget = new htmlweb(); var doc = webget.load(url); var root = doc.documentnode; var ages = root.selectnodes("//div[@class='infobox']/table/tr[5]/td/img"); list<string> tournamentages = new list<string>(); string agegroups = ""; list<string> agestring = new list<string>(); (int32 = 0; < ages.count(); i++) { if (ages[i].getattributevalue("src", "nope") == "images/2016/u6_yes.gif") { if (!agestring.contains(" u6 ")) { agestring.add(" u6 "); continue; } } else if (ages[i].getattributevalue("src", "nope") == "images/2016/u6_.gif") { continue; } if (ages[i].getattributevalue("src", "nope") == "images/2016/u7_yes.gif") { if (!agestring.contains(" u7 ")) { agestring.add(" u7 "); continue; } } else if (ages[i].getattributevalue("src", "nope") == "images/2016/u7_.gif") { continue; } if (ages[i].getattributevalue("src", "nope") == "images/2016/u8_yes.gif") { if (!agestring.contains(" u8 ")) { agestring.add(" u8 "); continue; } } else if (ages[i].getattributevalue("src", "nope") == "images/2016/u8_.gif") { continue; } // checks until u16.gif foreach (string in agestring) { if (a != "") { agegroups += a; } } agestring.clear(); if (agegroups != "") { tournamentages.add(agegroups); } agegroups = ""; } } }
to clear, i'm having trouble loop logic.
the flow goes this:
loop through current list of images if > u6_yes.gif concatenate u6 agestring else continue
however continue start , stuck in infinite loop, how can make gracefully handle when u6_.gif gone, ignore , go next?
why don't simplify loop this?
if (ages[i].getattributevalue("src", "nope") == "images/2016/u6_yes.gif") { if (!agestring.contains(" u6 ")) { agestring.add(" u6 "); continue; } } if (ages[i].getattributevalue("src", "nope") == "images/2016/u7_yes.gif") { if (!agestring.contains(" u7 ")) { agestring.add(" u7 "); continue; } } } (...)
just remove else if
blocks...
also, should consider extraction src
attribute ages array. can use linq , make loop lot simplier. this:
list<string> agestring = new list<string>(); list<string> imagesources = ages.where(x => x.getattributevalue("src", "nope").startswith("images/2016/u") && x.getattributevalue("src", "nope").endswith("_yes.gif")).tolist(); foreach (var src in imagesources) { agestring.add(" " + src.substring(11, 2).toupper() + " "); }
Comments
Post a Comment