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.





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 typeAvailable operations
HTML and XHTMLParse, create, edit, navigate, merge, convert, and render
MHTML and MHTLoad archives, convert, render, merge, and create updated archives
EPUBLoad publications, merge content, and render to fixed-layout or image output
MarkdownParse, create, edit, convert to HTML, and render through an HTML document
SVGLoad, 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, and XpsSaveOptions configure 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.



  

Support and Learning Resources

  
  

Aspose.HTML provides HTML processing APIs for other popular development platforms.