Html Agility Pack foreach循环错误

时间:2013-11-24 14:20:49

标签: c# oop html-agility-pack

string searchString = textBox1.Text.Replace(" ", "%20");
string url = "http://sometorrentsearchurl.com/search/" + searchString + "/0/99/401";

HttpWebRequest oReq = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse resp = (HttpWebResponse)oReq.GetResponse();

var doc = new HtmlAgilityPack.HtmlDocument();
doc.Load(resp.GetResponseStream());

foreach (HtmlNode torrent in doc.DocumentNode.SelectNodes("//tr"))
{
   foreach (HtmlNode title in torrent.SelectNodes(".//a[@class='detLink']"))
       {
          Label tTitle = new Label();
          tTitle.Text = title.InnerText;
          tTitle.Location = new Point(133, tHeightLoc);
          tTitle.BackColor = Color.Transparent;
          tTitle.ForeColor = Color.White;
          tTitle.AutoSize = false;
          tTitle.Font = new Font("Arial", 10);
          tTitle.Size = new Size(347, 25);
          tTitle.TextAlign = ContentAlignment.MiddleLeft;
          tTitle.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
          panel2.Controls.Add(tTitle);

          tHeightLoc += 45;
       }
 }

我正在尝试从网站获取种子列表,并且对于发现的每个html th标记,我想在我的表单中创建一些控件,其中的值来自其他子html标记,但此行返回错误foreach (HtmlNode title in torrent.SelectNodes(".//a[@class='detLink']"))

我想知道如何修复它,因为这是我第一次使用Html Agility Pack。

1 个答案:

答案 0 :(得分:0)

问题出在torrent.SelectNodes(".//a[@class='detLink']")),这是一个空选择,我修复了它torrent.SelectNodes("//a[@class='detLink']"))