问题描述
publicGuoJiPriceGetGuoJiPriceInfo(stringurl){try{stringnow=DateTime.Now.ToString("yyyy-MM-dd");url=string.Format("{0}_{1}_{1}_1.htm",url,now);stringform=Http.GetHtml(url,refcookie);//获取页面form=Other.GetRegValue("<tr><td((?!</tr>).)+",form);//页面内容所在区域MatchCollectionmatches=Other.GetRegValues("<td((?!</td>).)+",form);//要获取的内容foreach(Matchminmatches){stringa=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'> ",""));].Value;//获取第一个匹配到的内容stringb=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'> ",""));].Value;//stringc=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'> ",""));].Value;//stringd=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'> ",""));].Value;//returnnewGuoJiPrice(a,b,c,d,now);}}catch(Exceptionex){Console.WriteLine(ex.Message);returnnull;}}
解决方案
解决方案二:
catch(Exceptionex){Console.WriteLine(ex.Message);returnnull;}returnnull;------------------加上一句
解决方案三:
returnnewGuoJiPrice(a,b,c,d,now);这是不行的。当matches为空,返回什么呢?必须有一个写在foreach之外。
解决方案四:
不确定你是要一个对象还是一个对象集合回来,如果你只返回一个集合可以向下面这样改一下.但是感觉你应该需要返回一个集合。publicGuoJiPriceGetGuoJiPriceInfo(stringurl){GuoJiPriceresult=null;try{stringnow=DateTime.Now.ToString("yyyy-MM-dd");url=string.Format("{0}_{1}_{1}_1.htm",url,now);stringform=Http.GetHtml(url,refcookie);//获取页面form=Other.GetRegValue("<tr><td((?!</tr>).)+",form);//页面内容所在区域MatchCollectionmatches=Other.GetRegValues("<td((?!</td>).)+",form);//要获取的内容foreach(Matchminmatches){stringa=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'> ",""));].Value;//获取第一个匹配到的内容stringb=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'> ",""));].Value;//stringc=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'> ",""));].Value;//stringd=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'> ",""));].Value;//result=GuoJiPrice(a,b,c,d,now);break;}}catch(Exceptionex){Console.WriteLine(ex.Message);result=null;}retrunresult;}
解决方案五:
你这个函数是要有返回值的,但是你的程序流程不是每个分支都有返回值。
解决方案六:
...stringa=string.Empty;stringb=string.Empty;stringc=string.Empty;stringd=string.Empty;foreach(Matchminmatches){a=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'> ",""));].Value;//获取第一个匹配到的内容b=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'> ",""));].Value;//c=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'> ",""));].Value;//d=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'> ",""));].Value;//}returnnewGuoJiPrice(a,b,c,d,now);...
解决方案七:
测试了一下,发现还是只能获取到整个表格的第一行的数据。不能循环。我之前的代码:publicGuoJiPriceGetGuoJiPriceInfo(stringurl){try{stringnow=DateTime.Now.ToString("yyyy-MM-dd");url=string.Format("{0}_{1}_{1}_1.htm",url,now);stringform=Http.GetHtml(url,refcookie);form=Other.GetRegValue("<tr><td((?!</tr>).)+",form);MatchCollectionmatches=Other.GetRegValues("<td((?!</td>).)+",form);stringa=Other.GetRegValue(@".*",matches[1].Value);stringb=Other.GetRegValue(@".*",matches[2].Value);stringc=Other.GetRegValue(@".*",matches[3].Value);stringd=Other.GetRegValue(@".*",matches[4].Value);returnnewGuoJiPrice(a,b,c,d,now);}catch(Exceptionex){Console.WriteLine(ex.Message);returnnull;}}
这样我能正常获取到这张表格的第一行,匹配到的4个单元格的数据。但是:假如这个表格有10行的时候,我只能读取到第一行的数据,我怎样才能读取到这整10行的数据?
解决方案八:
应该怎么弄?
解决方案九:
引用楼主的回复:
C#codepublicGuoJiPriceGetGuoJiPriceInfo(stringurl){try{stringnow=DateTime.Now.ToString("yyyy-MM-dd");url=string……
说句心里话,你写的这个虽然说可以,但是真不能这么写的,怎么可以把返回值放到循环里
解决方案十:
建议,循环里只进行赋值,方法最后,返回。你现在的代码,循环进行1次就retrun了。并且若不执行循环,也没有“返回”,应该补上。这样就不会报错了。
解决方案十一:
publicList<GuoJiPrice>GetGuoJiPriceInfo(stringurl){List<GuoJiPrice>alllist=newList<GuoJiPrice>();GuoJiPriceresult=null;try{stringnow=DateTime.Now.ToString("yyyy-MM-dd");url=string.Format("{0}_{1}_{1}_1.htm",url,now);stringform=Http.GetHtml(url,refcookie);//获取页面form=Other.GetRegValue("<tr><td((?!</tr>).)+",form);//页面内容所在区域MatchCollectionmatches=Other.GetRegValues("<td((?!</td>).)+",form);//要获取的内容foreach(Matchminmatches){stringa=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'> ",""));].Value;//获取第一个匹配到的内容stringb=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'> ",""));].Value;//stringc=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'> ",""));].Value;//stringd=m.Groups[@".*",matches[1].Value.Replace("<tdalign='center'> ",""));].Value;//result=GuoJiPrice(a,b,c,d,now);alllist.add(result);}}catch(Exceptionex){Console.WriteLine(ex.Message);}retrunalllist;}
哎有时候自己多思考思考吧
解决方案十二:
在foreach的时候result应该new一下,在用之前result=newGuoJiPrice();
解决方案十三:
该回复于2012-04-01 10:25:44被版主删除
解决方案十四:
publicGuoJiPriceGetGuoJiPriceInfo(stringurl){GuoJiPriceresult=null;try{stringnow=DateTime.Now.ToString("yyyy-MM-dd");url=string.Format("{0}_{1}_{1}_1.htm",url,now);stringform=Http.GetHtml(url,refcookie);stringform2=Other.GetRegValue("<tablewidth='100%'border='0'cellspacing='0'cellpadding='0'class='ProTabmarginT'><tr((?!</table>).)+",form);MatchCollectiontrmatches=Other.GetRegValues("<tr><td((?!</tr>).)+",form2);foreach(Matchmatchintrmatches){MatchCollectionmatches=Other.GetRegValues("<td((?!</td>).)+",match.Value);stringa=Other.GetRegValue(@".*",matches[1].Value.Replace("<tdalign='center'> ",""));stringb=Other.GetRegValue(@".*",matches[2].Value.Replace("<tdalign='center'> ",""));stringc=Other.GetRegValue(@".*",matches[3].Value.Replace("<tdalign='center'> ",""));stringd=Other.GetRegValue(@".*",matches[4].Value.Replace("<td><spanclass='down'>",""));MessageBox.Show(a);//这里获取到了2次数据,循环。result=newGuoJiPrice(a,b,c,d,now);}}catch(Exceptionex){Console.WriteLine(ex.Message);returnnull;}returnresult;}
返回写入数据库方法的时候,只获取到了一次数据,默认第一条数据publicstaticboolAddGuoJiPrice(intId,intClassId,GuoJiPriceguojiprices)//国际市场价格写入{try{MessageBox.Show(guojiprices.ZhongLiang);//这里只取到一次数据,也就是说跟没循环一样stringsql=string.Format("insertintoGuoJiJiaGe(ClassId,SmallId,Addtime,ZhongLiang,JiaGe,ChanDi,ZhangDie)values({1},{0},#{2}#)",ClassId,Id,guojiprices.AddTime);OleDbCommandcmd=newOleDbCommand(sql,con);returncmd.ExecuteNonQuery()>0;}catch(Exceptionex){Console.WriteLine(ex.Message);returnfalse;}}
解决方案十五:
你必须要判断每一个条件,每一种情况都要有返回值。