This article shows how to get started with HTML Agility Pack and provides code samples to see how web scraping can be done using this package in C#. For users who are unafamiliar with “HTML Agility Pack“, this is an agile HTML parser that builds a read/write DOM and supports plain XPATH or XSLT. In simple words, it is a .NET code library that allows you to parse “out of the web” files (be it HTML, PHP or aspx).
To make it simpler, you can scrape web pages present on the internet using this library.
How to Get HTML Agility Pack in your application
You can get HTML Agility Pack in your application using Nuget. To install it in your project, you can just use the following in the Package Manager Console.
- Install-Package HtmlAgilityPack
Read this
How to add Nuget packages in your project
After adding the reference via Nuget, you need to include the reference in your page using the following.
- using HtmlAgilityPack;
Load a Page From Internet
To load a page directly from the web, you can use the following code:
- HtmlWeb web = new HtmlWeb();
- HtmlDocument document = web.Load("http://www.c-sharpcorner.com");
After executing this 2 lines of code, we have the entire page of http://c-sharpcorner.com in a document object of HtmlDocument class.
Load a Page from a Saved Document
Several times we need to load a HTML document from a saved file from our hard disk. To load a HTML document from a saved file, we need to write the following code.
- HtmlDocument document2 = new HtmlDocument();
- document2.Load(@"C:\Temp\sample.txt");
At this point, we have the entire HTML parsed and loaded in the document2 object.
At this point, let us see a sample HTML that we're using in the following sample.txt file.
- <html>
- <head>
- </head>
- <body>
- <div id="div1">
- <a href="div1-a1">Link 1 inside div1</a>
- <a href="div1-a2">Link 2 inside div1</a>
- </div>
- <a href="a3">Link 3 outside all divs</a>
- <div id="div2">
- <a href="div2-a1">Link 1 inside div2</a>
- <a href="div2-a2">Link 2 inside div2</a>
- </div>
- </body>
- </html>
Get all Hyperlinks in a page
Once we have the HTML document loaded, let us see how to get all the hyperlinks from the page.
- HtmlDocument document2 = new HtmlDocument();
- document2.Load(@"C:\Temp\sample.txt")
- HtmlNode[] nodes = document2.DocumentNode.SelectNodes("//a").ToArray();
- foreach (HtmlNode item in nodes)
- {
- Console.WriteLine(item.InnerHtml);
- }
This will output the following text,

Select a specific div in a page
To get a specific div in a page, we will use the following code :
- HtmlDocument document2 = new HtmlDocument();
- document2.Load(@"C:\Temp\sample.txt")
- HtmlNode node = document2.DocumentNode.SelectNodes("//div[@id='div1']").First();
This code will select the div with the id "div1′ from the page and return in the Node. You can now iterate on the ChildNodes property of the HtmlNode class to get further child elements of the DOM element.
Select all Hyperlinks within a specific div
To select all hyperlinks within a specific div, we can use the following 2 ways,
- HtmlDocument document2 = new HtmlDocument();
- document2.Load(@"C:\Temp\sample.txt")
- //Approach 1
- HtmlNode node = document2.DocumentNode.SelectNodes("//div[@id='div1']").First();
- HtmlNode [] aNodes = node.SelectNodes(".//a").ToArray();
- //Approach 2
- HtmlNode [] aNodes2 = document2.DocumentNode.SelectNodes("//div[@id='div1']//a").ToArray();
The preceding code will give the following output,

Filter hyperlinks for certain conditions
In case you want to filter nodes based on conditions, you can also use LINQ to do any kind of query on the nodes and return your specific nodes. For example, the following code will return all the hyperlinks where the anchor tags contain "div2" in their link text.
- HtmlDocument document2 = new HtmlDocument();
- document2.Load(@"C:\Temp\sample.txt");
- HtmlNode[] nodes = document2.DocumentNode.SelectNodes("//a").Where(x=>x.InnerHtml.Contains("div2")).ToArray();
- foreach (HtmlNode item in nodes)
- {
- Console.WriteLine(item.InnerHtml);
- }
The preceding code will give the following output,

I hope this article gives you a head start with HTML Agility Pack. If you have any questions, please mention in the comments section.


Dinesh GabhanePosted Nov 12, 2019, 7:43 PM EST
Good One !!!
Ali BerroPosted Jul 25, 2019, 2:20 AM EDT
Hello, I am trying to parse data from a website but my problem is that I don't have the entire page of the page I want to parse, in a document object of HtmlDocument class. I only get a piece of the HTML that is in the page source so how can I get the full HTML in the document object
Swamy RPosted Feb 20, 2019, 5:42 PM EST
Yes, Thank you for the good tips on HTML Tags, I have been small doubts on this topic. Now I have a good vision on topic.
Raja SaqibPosted Oct 5, 2018, 5:17 AM EDT
How to scrap html/div tag from others web C#
balaji manoharanPosted May 7, 2018, 5:00 AM EDT
IF I want to print the html in server side, how can i do it?
Chaitanya PPosted Mar 27, 2018, 5:00 AM EDT
Hi, Can I set value to a text field, and click button that perform AJAX call . Then get data from result div
José Afonso BragaPosted Dec 6, 2017, 5:03 AM EST
Hi, is it possible to merge multiple HTML files saved on disk into a single HTML file with HtmlAgilityPack? Thanks in advance.p
ahmad kosasihPosted Aug 9, 2017, 10:58 PM EDT
Hi, is it possible to get html source with data that load using javascript dom. and how? thank you
Akshay AbhyankarPosted Jun 13, 2017, 9:29 PM EDT
Thankyou for the nice article. But this is all about printing and changing things on console. Can you explain how can we change the contents of the page on a button click.
Sushrut SathePosted Jun 7, 2017, 4:37 AM EDT
Hi, Is there any way to get the class attribute value, in short I am looking for getting value from "Document.getElementsByClassName" using this package??
Ravi PrakashPosted May 30, 2017, 8:57 PM EDT
Foreach (HtmlNode node in doc.DocumentNode.Descendants()) { if (node.Name != "#text" && node.Name != "#comment") { Console.WriteLine(node.Name); Console.WriteLine(node.GetAttributeValue("Id", true)); } I used the above code to get all nodes in the page in the Webpage. But I should also get all the attributes to that node like(class,name,type). I am also getting Xpath. I tried by using HASAttributes,and attributes. But not getting the required output.
Ravi PrakashPosted May 26, 2017, 3:10 AM EDT
How to get all the Nodes used in the web page and their id attribute value.
adi mojjadaPosted May 25, 2017, 1:23 AM EDT
How to change the default value in "//a" inside "//div" and load the html with new changed values??3
Upendra Pratap ShahiPosted Aug 24, 2016, 2:09 AM EDT
Nice one..
Nitesh KejriwalPosted Mar 11, 2016, 1:33 AM EST
Do you want to click inside the HTML parsed? If yes, I doubt, HTML Agility pack is not meant to do so.
amir mohamadiPosted Nov 2, 2015, 8:12 AM EST
Hi.Thanks very Much . I fix it . but Html code show like this picture : http://csharpcorner.mindcrackerinc.netdna-cdn.com/forums/uploadfile/ae2816/11022015130451PM/aaa2.png . how should I do to convert this codes to Real Html ?
amir mohamadiPosted Nov 2, 2015, 6:12 AM EST
hello . how should I Use HTML Agility Pack IN Asp.Net MVC 5.2? what should I do in model,view,controller ?
amir mohamadiPosted Nov 2, 2015, 6:10 AM EST
hello
satheeshPosted Sep 18, 2015, 1:04 AM EDT
what is the Exact Use of the HTML Agility Pack
Santhakumar MunuswamyPosted Jun 15, 2015, 10:29 AM EDT
Thanks for nice article:)
NitinPosted Jun 15, 2015, 6:26 AM EDT
nice
Former memberPosted Jun 15, 2015, 4:20 AM EDT
nice article
Sibeesh VenuTOP 100Posted Jun 15, 2015, 1:11 AM EDT
Good one.