Retrieve document content

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.html in 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, returns HtmlNode
HtmlNode childNode = itemNode.SelectSingleNode("//div[1]");Code language: JavaScript (javascript)
  • SelectNodes("XPath"):Matches all qualifying nodes, returns HtmlNodeCollection

3. Read Text and Attributes

  1. Get plain text inside node
string text = itemNode.InnerText;
  1. Read tag attribute (basic pattern)
string href = itemNode.Attributes["href"].Value;Code language: JavaScript (javascript)

Warning: If the tag has no href attribute, directly accessing Attributes["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

  1. Null Handling
    SelectNodes() returns null when zero nodes match. Always validate before foreach iteration:
if(newsList != null)
{
    foreach(var item in newsList){ ... }
}Code language: PHP (php)
  1. Differentiate InnerText / InnerHtml / OuterHtml
  • InnerText:Extracts only text content, strips all markup tags
  • InnerHtml:Inner HTML of node, excludes its own opening‑closing tag
  • OuterHtml:Self‑tag plus all inner HTML markup
  1. XPath Tips
    //div performs 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:

Leave a Reply

Your email address will not be published. Required fields are marked *