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 13, 2019, 12:43 AM
Good One !!!
Ali BerroPosted Jul 25, 2019, 6:20 AM
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, 10:42 PM
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, 9:17 AM
How to scrap html/div tag from others web C#
balaji manoharanPosted May 7, 2018, 9:00 AM
IF I want to print the html in server side, how can i do it?
Chaitanya PPosted Mar 27, 2018, 9:00 AM
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, 10:03 AM
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 10, 2017, 2:58 AM
Hi, is it possible to get html source with data that load using javascript dom. and how? thank you
Akshay AbhyankarPosted Jun 14, 2017, 1:29 AM
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, 8:37 AM
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 31, 2017, 12:57 AM
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, 7:10 AM
How to get all the Nodes used in the web page and their id attribute value.
adi mojjadaPosted May 25, 2017, 5:23 AM
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, 6:09 AM
Nice one..
Nitesh KejriwalPosted Mar 11, 2016, 6:33 AM
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, 1:12 PM
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, 11:12 AM
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, 11:10 AM
hello
satheeshPosted Sep 18, 2015, 5:04 AM
what is the Exact Use of the HTML Agility Pack
Santhakumar MunuswamyPosted Jun 15, 2015, 2:29 PM
Thanks for nice article:)
NitinPosted Jun 15, 2015, 10:26 AM
nice
Former memberPosted Jun 15, 2015, 8:20 AM
nice article
Sibeesh VenuPosted Jun 15, 2015, 5:11 AM
Good one.