public GuoJiPrice GetGuoJiPriceInfo(string url)
        {
            try
            {
                string now = DateTime.Now.ToString("yyyy-MM-dd");
                url = string.Format("{0}_{1}_{1}_1.htm", url, now);
                string form = Http.GetHtml(url, ref cookie);//获取页面
                form = Other.GetRegValue("<tr><td((?!</tr>).)+", form);//页面内容所在区域
                MatchCollection matches = Other.GetRegValues("<td((?!</td>).)+", form);//要获取的内容
                foreach (Match m in matches)
                {
                    string a = m.Groups[@".*", matches[1].Value.Replace("<td align='center'>&nbsp;", ""));].Value; //获取第一个匹配到的内容
                    string b = m.Groups[@".*", matches[1].Value.Replace("<td align='center'>&nbsp;", ""));].Value; //
                    string c = m.Groups[@".*", matches[1].Value.Replace("<td align='center'>&nbsp;", ""));].Value; //
                    string d = m.Groups[@".*", matches[1].Value.Replace("<td align='center'>&nbsp;", ""));].Value; //
                  return new GuoJiPrice(a, b, c, d, now);
                }            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
        }

解决方案 »

  1.   

                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return null;
                }
                return null;
    ------------------
    加上一句
      

  2.   

    return new GuoJiPrice(a, b, c, d, now);
    这是不行的。当matches为空,返回什么呢?必须有一个写在foreach之外。
      

  3.   

    不确定你是要一个对象还是一个对象集合回来,如果你只返回一个集合 可以向下面这样改一下.但是感觉你应该需要返回一个集合。public GuoJiPrice GetGuoJiPriceInfo(string url)
            {
                GuoJiPrice result=null;
                try
                {
                    string now = DateTime.Now.ToString("yyyy-MM-dd");
                    url = string.Format("{0}_{1}_{1}_1.htm", url, now);
                    string form = Http.GetHtml(url, ref cookie);//获取页面
                    form = Other.GetRegValue("<tr><td((?!</tr>).)+", form);//页面内容所在区域
                    MatchCollection matches = Other.GetRegValues("<td((?!</td>).)+", form);//要获取的内容
                    foreach (Match m in matches)
                    {
                        string a = m.Groups[@".*", matches[1].Value.Replace("<td align='center'>&nbsp;", ""));].Value; //获取第一个匹配到的内容
                        string b = m.Groups[@".*", matches[1].Value.Replace("<td align='center'>&nbsp;", ""));].Value; //
                        string c = m.Groups[@".*", matches[1].Value.Replace("<td align='center'>&nbsp;", ""));].Value; //
                        string d = m.Groups[@".*", matches[1].Value.Replace("<td align='center'>&nbsp;", ""));].Value; //
                      result= GuoJiPrice(a, b, c, d, now);
                      break;
                    }            }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    result=null;
                }
             retrun result;
            }
      

  4.   


    ...
    string a = string.Empty;
    string b = string.Empty;
    string c = string.Empty;
    string d = string.Empty;
     foreach (Match m in matches)
                    {
                        a = m.Groups[@".*", matches[1].Value.Replace("<td align='center'>&nbsp;", ""));].Value; //获取第一个匹配到的内容
                         b = m.Groups[@".*", matches[1].Value.Replace("<td align='center'>&nbsp;", ""));].Value; //
                        c = m.Groups[@".*", matches[1].Value.Replace("<td align='center'>&nbsp;", ""));].Value; //
                        d = m.Groups[@".*", matches[1].Value.Replace("<td align='center'>&nbsp;", ""));].Value; //
                      
                    }
    return new GuoJiPrice(a, b, c, d, now);
    ...
      

  5.   

    测试了一下,发现还是只能获取到整个表格的第一行的数据。 不能循环。我之前的代码:public GuoJiPrice GetGuoJiPriceInfo(string url)
            {
                try
                {
                    string now = DateTime.Now.ToString("yyyy-MM-dd");
                    url = string.Format("{0}_{1}_{1}_1.htm", url, now);
                    string form = Http.GetHtml(url, ref cookie);
                    form = Other.GetRegValue("<tr><td((?!</tr>).)+", form);                MatchCollection matches = Other.GetRegValues("<td((?!</td>).)+", form);
                    string a = Other.GetRegValue(@".*", matches[1].Value);
                    string b = Other.GetRegValue(@".*", matches[2].Value);
                    string c = Other.GetRegValue(@".*", matches[3].Value);
                    string d = Other.GetRegValue(@".*", matches[4].Value);
                    return new GuoJiPrice(a, b, c, d, now);            }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return null;
                }
            }这样我能正常获取到这张表格的第一行,匹配到的4个单元格的数据。但是:假如这个表格有10行的时候,我只能读取到第一行的数据,我怎样才能读取到这整10行的数据?
      

  6.   

    建议,循环里只进行赋值,方法最后,返回。
    你现在的代码,循环进行1次就retrun了。并且若不执行循环,也没有“返回”,应该补上。这样就不会报错了。
      

  7.   


    public List<GuoJiPrice> GetGuoJiPriceInfo(string url)
            {
                List<GuoJiPrice> alllist=new List<GuoJiPrice>();
                GuoJiPrice result=null;
                try
                {
                    string now = DateTime.Now.ToString("yyyy-MM-dd");
                    url = string.Format("{0}_{1}_{1}_1.htm", url, now);
                    string form = Http.GetHtml(url, ref cookie);//获取页面
                    form = Other.GetRegValue("<tr><td((?!</tr>).)+", form);//页面内容所在区域
                    MatchCollection matches = Other.GetRegValues("<td((?!</td>).)+", form);//要获取的内容
                    foreach (Match m in matches)
                    {
                        string a = m.Groups[@".*", matches[1].Value.Replace("<td align='center'>&nbsp;", ""));].Value; //获取第一个匹配到的内容
                        string b = m.Groups[@".*", matches[1].Value.Replace("<td align='center'>&nbsp;", ""));].Value; //
                        string c = m.Groups[@".*", matches[1].Value.Replace("<td align='center'>&nbsp;", ""));].Value; //
                        string d = m.Groups[@".*", matches[1].Value.Replace("<td align='center'>&nbsp;", ""));].Value; //
                      result= GuoJiPrice(a, b, c, d, now);
                      alllist.add(result);
                    }            }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    
                }
             retrun alllist;
            }
    哎 有时候自己多思考思考吧
      

  8.   

    在 foreach 的时候 result 应该new 一下,在用之前
    result= new GuoJiPrice();
      

  9.   

    public GuoJiPrice GetGuoJiPriceInfo(string url)
            {
                GuoJiPrice result = null;
                try
                {
                    string now = DateTime.Now.ToString("yyyy-MM-dd");
                    url = string.Format("{0}_{1}_{1}_1.htm", url, now);
                    string form = Http.GetHtml(url, ref cookie);
                    string form2 = Other.GetRegValue("<table width='100%' border='0' cellspacing='0' cellpadding='0' class='ProTab marginT'><tr((?!</table>).)+",form);
                    MatchCollection trmatches = Other.GetRegValues("<tr><td((?!</tr>).)+", form2);
                   foreach (Match match in trmatches)
                    {
                        MatchCollection matches = Other.GetRegValues("<td((?!</td>).)+", match.Value);
                       string a = Other.GetRegValue(@".*", matches[1].Value.Replace("<td align='center'>&nbsp;", ""));
                       string b = Other.GetRegValue(@".*", matches[2].Value.Replace("<td align='center'>&nbsp;", ""));
                       string c = Other.GetRegValue(@".*", matches[3].Value.Replace("<td align='center'>&nbsp;", ""));
                       string d = Other.GetRegValue(@".*", matches[4].Value.Replace("<td><span class='down'>", " "));
                       MessageBox.Show(a);//这里获取到了2次数据,循环。
                       result = new GuoJiPrice(a, b, c, d, now);                }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return null;
                }
                return result;
            }返回写入数据库方法的时候,只获取到了一次数据,默认第一条数据public static bool AddGuoJiPrice(int Id, int ClassId, GuoJiPrice guojiprices)//国际市场价格写入
            {
               try
                {
                    MessageBox.Show(guojiprices.ZhongLiang);//这里只取到一次数据,也就是说跟没循环一样
                    string sql = string.Format("insert into GuoJiJiaGe(ClassId,SmallId,Addtime,ZhongLiang,JiaGe,ChanDi,ZhangDie) values({1},{0},#{2}#)", ClassId, Id, guojiprices.AddTime);
                    OleDbCommand cmd = new OleDbCommand(sql, con);
                   return cmd.ExecuteNonQuery() > 0;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return false;
                }
            }