Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.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# HtmlAlityPack selectsinglenode返回null_C#_Html_Html Agility Pack_Selectsinglenode - Fatal编程技术网

C# HtmlAlityPack selectsinglenode返回null

C# HtmlAlityPack selectsinglenode返回null,c#,html,html-agility-pack,selectsinglenode,C#,Html,Html Agility Pack,Selectsinglenode,问题是,它每20次左右就能正常运行一次,而且完全是随机的。无法处理这种情况。。。非常感谢您的帮助^^ WebClient client = new WebClient(); client.Encoding = Encoding.UTF8; try { html = client.DownloadString(url); } catch (WebException)

问题是,它每20次左右就能正常运行一次,而且完全是随机的。无法处理这种情况。。。非常感谢您的帮助^^

        WebClient client = new WebClient();

        client.Encoding = Encoding.UTF8;
        try
        {
            html = client.DownloadString(url);
        }
        catch (WebException)
        {
            if (MessageBox.Show("url'de hata", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK)
            {

            }

        }

        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        HtmlWeb web = new HtmlWeb();

        doc.LoadHtml(html);

        HtmlNode foo = doc.DocumentNode.SelectSingleNode(Xpath);    

        Listbox1.Items.Add(foo.Attributes["href"].Value); //error here 
        currentPage = (foo.Attributes["href"].Value);

您必须检查节点是否不为null。服务器可能会为每个请求返回不同的响应。服务器可能会返回所需的20对1元素。你确定内容是一样的吗?会话可能因情况而异。确保成功和失败的内容相等。

请在其中包括一个HTML示例,在其中起作用,在其中不起作用。如果我们没有这些信息,除了表示同情,我们还能做些什么?我很怀疑这种方法是否有问题。这是有原因的。问题是它在同一个HTM中工作和不工作。我不相信你。你做了什么来确认它总是完全相同的HTML,字符对字符?如果你的答案是“我认为应该是”,那就不是正确的答案。但是,向我们展示HTML以便我们自己查看。只有当
DownloadString()
不总是返回相同的文档时,才可能发生这种情况。这是完全可能的,怎么可能呢?