Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Html Agility Pack foreach循环错误_C#_Oop_Html Agility Pack - Fatal编程技术网

C# Html Agility Pack foreach循环错误

C# Html Agility Pack foreach循环错误,c#,oop,html-agility-pack,C#,Oop,Html Agility Pack,我试图从一个站点获取torrent列表,对于找到的每个htmlth标记,我想用其他子html标记的值在表单中创建一些控件,但此行返回一个错误foreach(torrent.SelectNodes中的HtmlNode title(“.//a[@class='detLink']) 我想知道如何修复它,因为这是我第一次使用Html Agility Pack。问题出在这里torrent.SelectNodes(“.//a[@class='detLink']”),这是一个空选择,我像这样修复了它torre

我试图从一个站点获取torrent列表,对于找到的每个html
th
标记,我想用其他子html标记的值在表单中创建一些控件,但此行返回一个错误
foreach(torrent.SelectNodes中的HtmlNode title(“.//a[@class='detLink'])


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

问题出在这里
torrent.SelectNodes(“.//a[@class='detLink']”)
,这是一个空选择,我像这样修复了它
torrent.SelectNodes(//a[@class='detLink']”)
,无需共享错误详细信息,因为我们是读心术者。@Oded对象引用未设置为对象的实例。那么您尝试引用的对象为空。具体发生在哪一行?@ChrisK
foreach(torrent.SelectNodes(“.//a[@class='detLink']”)中的HtmlNode标题)
变量torrent可能为空。调试时检查它
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;
       }
 }