HTML Navigation | C#
Using Aspose.HTML class library, you can easily create your own application, since our API provides a powerful toolset to analyze and collect information from HTML documents.
This article provides information on how to programmatically extract data from HTML documents with the Aspose.HTML API. You find out:
- how to navigate through an HTML document and perform a detailed inspection of its elements using the API;
- about custom filters usage for iterating over the document elements;
- how to navigate over the document by using CSS Selector or XPath Query.
HTML navigation
We consider how the DOM represents an HTML document in memory and how to use API for navigation through HTML files. Many ways can be used to make HTML navigation. The following shortlist shows the simplest way to access all DOM elements:
Property | Description |
---|---|
FirstChild | Accessing this property of an element must return a reference to the first child node. |
LastChild | Accessing this property of an element must return a reference to the last child node |
NextSibling | Accessing this property of an element must return a reference to the sibling node of that element which most immediately follows that element. |
PreviousSibling | Accessing this property of an element must return a reference to the sibling node of that element which most immediately precedes that element. |
ChildNodes | Returns a list that contains all children of that element. |
Four of the Node class properties - FirstChild, LastChild, NextSibling, and PreviousSibling, each provides a live reference to another element with the defined relationship to the current element if the related element exists. For a complete list of classes and methods represented in the Aspose.Html.Dom Namespace, please visit API Reference Source .
Using the mentioned properties, you can navigate through an HTML document as it follows:
Inspection of the HTML Document and its Elements
Aspose.HTML contains a list of methods that are based on the Element Traversal Specifications . You can perform a detailed inspection of the document and its elements using the API. The following code sample shows the generalized usage of Element Traversal features.
Note: You need to specify the path to the source HTML file in your local file system (documentPath
).
The
DocumentElement
property of the Document class gives direct access to the <html>
element of the document (
html_file.html
). The LastElementChild property of the Document class returns the last child element of the <html>
element. It is the <body>
element. According to the code snippet above, the variable “element” is overloaded again, and the FirstElementChild property returns the first child of the <body>
element. It is the <h1>
element.
Custom Filter Usage
For the more complicated scenarios, when you need to find a node based on a specific pattern (e.g., get the list of headers, links, etc.), you can use a specialized TreeWalker or NodeIterator object with a custom Filter implementation.
The following example shows how to implement your own NodeFilter to skip all elements except images:
Once you implement a filter, you can use HTML navigation as it follows:
XPath Query
The alternative to the HTML Navigation is XPath Query ( XML Path Language ) that often referred to simply as an XPath. It is a query language that can be used to query data from HTML documents. It is based on a DOM representation of the HTML document, and selects nodes by various criteria. The syntax of the XPath expressions is quite simple, and what is more important, it is easy to read and support.
The following example shows how to use XPath queries within Aspose.HTML API:
CSS Selector
Along with HTML Navigation and XPath, you can use CSS Selector API that is also supported by our library. This API is designed to create a search pattern to match elements in a document tree based on CSS Selectors syntax.
In the following example, we use the QuerySelectorAll() method for navigation through an HTML document and search the needed elements. This method takes as a parameter the query selector and returns a NodeList of all the elements, which match the specified selector.
You can download the complete C# examples and data files from GitHub .
Aspose.HTML offers free online Data Scrapers Apps that are a way to get data from websites. Using our web extracting tools, you’re able to get data from the Internet in seconds. Our Apps are safe, work on any platform and do not require any software installation.