如何删除 htmldocument.cs 未找到 html 敏捷包中的错误

how to remove htmldocument.cs not found error in html agility pack

class 响应:

public string WebResponse(string url) //class through which i'll have link of website and will parse some divs in method of this class
{
    string html = string.Empty;
    try
    {
        HtmlDocument doc = new HtmlDocument(); //when code comes here it gives an error htmldocument.cs not found,and open window for browsing source
        WebClient client = new WebClient(); // even if i put htmlWeb there it still look for HtmlWeb.cs not found
        html = client.DownloadString(url); //is this from some breakpoint error coz i set only one in method where i am parsing,
        doc.LoadHtml(html);
    }
    catch (Exception)
    {
        html = string.Empty;
    }

    return html; //please help me to remove this error using html agility pack with console application 
}

即使我创建了新项目和 运行 代码它卡在这里并且我也添加了 DLL 仍然给我这个错误请帮我删除这个错误

WebResponse 是一个抽象 class 这意味着它首先是一个保留字。第二 - 为了使用 WebResponse,class 必须从 WebResponse 继承,即

public class WR : WebResponse
{
    //Code
}

还有。您当前的代码与 Html Agility Pack 无关。如果您想将网页的 html 加载到 Html 文档中 - 请执行以下操作:

HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();

try{
    var temp = new Uri(url);
    var request = (HttpWebRequest)WebRequest.Create(temp);
    request.Method = "GET";
    using (var response = (HttpWebResponse)request.GetResponse())
    {
        using (var stream = response.GetResponseStream())
        {
            htmlDoc.Load(stream, Encoding.GetEncoding("iso-8859-9"));
        }
    }
}catch(WebException ex){
    Console.WriteLine(ex.Message);
}

然后为了获取 Html 文档中的节点,您必须像这样使用 xPath:

HtmlNode node = htmlDoc.DocumentNode.SelectSingleNode("//body");
Console.WriteLine(node.InnerText);

该错误有时是因为您使用的 Nuget html 敏捷包的版本,请在 visual studio 库中更新您的 nuget,然后尝试安装 html 敏捷包和 运行 在你的项目中

您可以尝试清理并重新构建 solution.This 可能会解决问题。