Parse, Edit, and Convert HTML in C#
Aspose.HTML for .NET is a class library for parsing and processing HTML-based content in C# applications. The HTMLDocument class loads HTML from a file, URL, string, or stream and exposes the document through a standards-based DOM. Applications can inspect or edit elements, work with CSS, SVG, and HTML Canvas, and use the Converter class to produce PDF, XPS, DOCX, and raster image output. The same API also processes XHTML, MHTML, EPUB, and Markdown workflows.
Choose an HTML Processing Task
Choose a workflow that matches the application. Each guide explains the relevant API classes, provides focused C# examples, and links to detailed documentation and API Reference pages.
Create and load HTML documents
Build a document from markup or DOM nodes, or load existing HTML from a file, URL, string, or stream.
Find and extract HTML elements
Locate content with CSS selectors or XPath queries and read matching DOM nodes in C#.
Edit web documents
Update elements, attributes, text, styles, and linked resources in HTML, MHTML, and Markdown workflows.
Convert HTML to output formats
Render HTML and XHTML as PDF, XPS, DOCX, TIFF, PNG, JPEG, and other supported formats.
Convert EPUB, MHTML, Markdown, and SVG
Use source-specific conversion methods and save options for common web and publishing formats.
Merge web documents
Combine several HTML, MHTML, EPUB, or Markdown sources into one rendered document in the required order.
Generate HTML code
Create markup for forms, media, tables, links, interactive elements, and semantic text in online examples.
newValidate web accessibility
Run WCAG checks, inspect color contrast, verify image alternative text, and export validation results.
Manage images in HTML
Add, remove, resize, or extract images while preserving the surrounding document structure and content.
Create and edit HTML tables
Build tables with the DOM API, extract rows and cells, or update existing table content in C#.
Populate HTML templates
Combine an HTML template with XML or JSON data to generate documents from reusable layouts.
Render HTML Canvas 2D
Draw shapes, text, paths, and images with the Canvas 2D API and render the result with the document.
Convert CSS length units
Use the `Unit` API to convert CSS pixels, centimeters, and millimeters in C# calculations.
newUse cross-platform drawing on Linux
Add the Aspose.HTML.Drawing package when Linux applications require drawing without System.Drawing.Common.
Install Aspose.HTML for .NET
Add the Aspose.HTML NuGet package to a .NET project. The package provides the HTML DOM, CSS, rendering, conversion, and document-processing namespaces used throughout the examples and guides on this site.
Install Aspose.HTML with the .NET CLI
dotnet add package Aspose.HTML
Parse HTML and Select Elements in C#
Load an HTML file with HTMLDocument , use QuerySelectorAll() to find matching elements, and read each result through the DOM. The example uses explicit Aspose.HTML types and can be placed in a console application after installing the NuGet package.
Parse HTML in C#
using Aspose.Html;
using Aspose.Html.Collections;
using Aspose.Html.Dom;
// Load the HTML document
using HTMLDocument document = new HTMLDocument("input.html");
// Select every link in the document
NodeList links = document.QuerySelectorAll("a");
foreach (Element link in links)
{
Console.WriteLine($"{link.TextContent}: {link.GetAttribute("href")}");
}
QuerySelectorAll("a") returns a NodeList containing every matching anchor element in document order. Use QuerySelector() when only the first matching element is required, or use
XPath Query
for XPath expressions.
Supported Web Formats and Output Types
| Input or document type | Available operations |
|---|---|
| HTML and XHTML | Parse, create, edit, navigate, merge, convert, and render |
| MHTML and MHT | Load archives, convert, render, merge, and create updated archives |
| EPUB | Load publications, merge content, and render to fixed-layout or image output |
| Markdown | Parse, create, edit, convert to HTML, and render through an HTML document |
| SVG | Load, inspect, edit, and convert vector content |
Conversion workflows can produce PDF, XPS, DOCX, TIFF, PNG, JPEG, BMP, and GIF output where supported by the selected source format. Read the format conversion documentation to choose the correct Converter method and save-options class.
HTML API Classes and Developer Resources
- HTMLDocument represents a loaded or newly created HTML document and provides access to the DOM.
- QuerySelector() and QuerySelectorAll locate elements with CSS selectors.
- Converter provides static methods for HTML, XHTML, MHTML, EPUB, Markdown, and SVG conversion.
- PdfSaveOptions
,
DocSaveOptions,ImageSaveOptions, andXpsSaveOptionsconfigure format-specific output.
Continue with the getting started documentation , browse the complete Aspose.HTML for .NET API Reference , or review runnable examples in the Aspose.HTML for .NET GitHub repository .
Frequently Asked Questions
1. What can I do with an HTML document after loading it in C#?
After loading HTML into an HTMLDocument, you can inspect the DOM, find elements with CSS selectors or XPath, change content and attributes, add or remove nodes, and then save or convert the updated document.
2. Can I modify HTML before converting it to PDF or an image?
Yes. Load the source into an HTMLDocument, update its DOM, CSS, or linked content, and pass the modified document to the required conversion method. The generated PDF, XPS, DOCX, or image reflects those changes.
3. How are relative CSS, image, and font URLs resolved when loading HTML?
Relative resource URLs are resolved against the document address or the base URL supplied when the HTML is loaded. A correct base URL is important when markup comes from a string or stream and references external resources.
4. Which API method should I use to convert HTML, MHTML, EPUB, Markdown, or SVG?
Use the source-specific method provided by the Converter class, such as ConvertHTML(), ConvertMHTML(), ConvertEPUB(), ConvertMarkdown(), or ConvertSVG(). The selected save-options class determines the output format and conversion settings.
Explore Aspose.HTML for .NET Guides
Continue with focused API guides for the most common HTML processing tasks.
Support and Learning Resources
- Developer Resources
- Documentation
- GitHub Examples
- API Reference
- Product Support
- Free Support
- Paid Support
- Blog
- Release Notes
- Why Aspose.HTML for .NET
- Aspose Customers
- Success Stories