如何在html敏捷包中删除htmldocument.cs not found错误

c# html-agility-pack

类响应:

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仍然它给我这个错误请帮我删除此错误

一般承认的答案

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


热门答案

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

public class WR : WebResponse
{
    //Code
}

也。您当前的代码与Html Agility Pack无关。如果要将网页的html加载到HtmlDocument中,请执行以下操作:

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);



许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因
许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因