如何在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合法嗎? 是的,了解原因