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

c# html-agility-pack

Question

class Response:

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 
}

even if i make new project and run code it stuck here and i have added DLL too still it is giving me this error please help me to remove this error

Accepted Answer

that error is sometimes because of version of you are using of Nuget html agility pack,update your nuget in the visual studio gallery then try installing html agility pack and run in your project


Popular Answer

WebResponse is an abstract class meaning it is a reserved word first of all. Second - In order to use WebResponse a class has to inherit from WebResponse ie.

public class WR : WebResponse
{
    //Code
}

Also. Your current code has nothing to with Html Agility Pack. If you want to load the html of a webpage into a HtmlDocument - do the following:

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

Then in order to get nodes in the Html Document you have to use xPath like so:

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


Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why