Get Document Object
1. Create Document Object
HtmlDocument document = new HtmlDocument();Code language: JavaScript (javascript)
2. Two Loading Methods
1)Load HTML string
document.LoadHtml(htmlString);Code language: JavaScript (javascript)
2)Load local HTML file (specify encoding to avoid garbled text)
document.Load(@"c:/news.html", Encoding.UTF8);Code language: JavaScript (javascript)
For Windows paths, it’s recommended to use
@to disable escaping;c://news.htmlin the material is just a writing example.
3. Get Root Node
HtmlNode rootNode = document.DocumentNode;Code language: JavaScript (javascript)
All xpath queries run against rootNode.
Node Operations
1. Create New Node From Node HTML
HtmlNode newNode = HtmlNode.CreateNode(newsNode.OuterHtml);
OuterHtml:Includes self‑tag plus all inner HTML content- Commonly used for node duplication and parsing partial HTML snippets
2. Query Nodes
SelectSingleNode("XPath"):Matches the first node, returnsHtmlNode
HtmlNode childNode = itemNode.SelectSingleNode("//div[1]");Code language: JavaScript (javascript)
SelectNodes("XPath"):Matches all qualifying nodes, returnsHtmlNodeCollection
3. Read Text and Attributes
- Get plain text inside node
string text = itemNode.InnerText;
- Read tag attribute (basic pattern)
string href = itemNode.Attributes["href"].Value;Code language: JavaScript (javascript)
Warning: If the tag has no
hrefattribute, directly accessingAttributes["href"]yields null and triggers runtime exceptions.
Safe approach recommended:
string href = itemNode.GetAttributeValue("href", "");Code language: JavaScript (javascript)
4. Full Sample: Iterate News List
foreach (HtmlNode item in newsList)
{
string title = item.InnerText;
string href = item.Attributes["href"].Value;
}Code language: PHP (php)
Additional Notes
- Null Handling
SelectNodes()returnsnullwhen zero nodes match. Always validate before foreach iteration:
if(newsList != null)
{
foreach(var item in newsList){ ... }
}Code language: PHP (php)
- Differentiate InnerText / InnerHtml / OuterHtml
InnerText:Extracts only text content, strips all markup tagsInnerHtml:Inner HTML of node, excludes its own opening‑closing tagOuterHtml:Self‑tag plus all inner HTML markup
- XPath Tips
//divperforms global search for all div elements; omit//to match only direct child nodes.
Complete Working Example
using HtmlAgilityPack;
using System.Text;
//1.Initialize document
HtmlDocument document = new HtmlDocument();
document.Load("news.htm", Encoding.UTF8);
HtmlNode rootNode = document.DocumentNode;
//2.Batch fetch news links
HtmlNodeCollection newsList = rootNode.SelectNodes(@"//div[@class='blk122']/a");
if(newsList != null)
{
foreach (HtmlNode item in newsList)
{
string title = item.InnerText.Trim();
string link = item.GetAttributeValue("href", "");
Console.WriteLine($"Title:{title} Link:{link}");
}
}Code language: JavaScript (javascript)
Retrieve document content
Previous: Practical Usage