Extract Tables from HTML Files with a .NET API
Aspose.HTML for .NET
loads an HTML file into an
HTMLDocument
and exposes its tables through the DOM. A C# application can select one or every <table> element, read its OuterHTML, and create a separate HTML document containing the complete table markup.
Extract All Tables from an HTML File in C#
The following example selects every table in input.html and saves each one to a numbered HTML file. The source document’s base URI is passed to each output document so relative resource URLs can be resolved from their original location while the table is processed.
C# Code to Extract HTML Tables
using Aspose.Html;
using Aspose.Html.Dom;
using System;
// Load the source HTML file
using HTMLDocument document = new HTMLDocument("input.html");
int tableNumber = 1;
foreach (Element table in document.QuerySelectorAll("table"))
{
// Save each complete table to a separate HTML file
string outputPath = $"table-{tableNumber}.html";
using HTMLDocument tableDocument = new HTMLDocument(table.OuterHTML, document.BaseURI);
tableDocument.Save(outputPath);
tableNumber++;
}
if (tableNumber == 1)
{
// Report that the selector returned no tables
Console.WriteLine("No tables were found in the HTML document.");
}
Steps to Extract Tables from HTML
- Load the source file with an
HTMLDocumentconstructor. - Select the table elements with
QuerySelectorAll("table"). - Iterate through the returned elements and read each table’s
OuterHTML. - Create a new
HTMLDocumentfrom the extracted markup and the source document’s base URI. - Call
Save()with a unique output file name for every extracted table.
HTML Table Extraction Options and Limitations
- Choose the extraction scope. Use
tableto select every table or a narrower selector such as#price-table,.results table, ormain tableto limit the result. - Preserve the table markup.
OuterHTMLincludes the selected<table>element, its attributes, rows, cells, and nested content. - Keep resource resolution context. Passing
document.BaseURIallows relative URLs inside the extracted markup to be interpreted against the source document during processing. - Handle styles separately. Inline styles and
styleattributes inside the table remain in its markup. Rules stored in the source document’s<head>or external stylesheets are not copied into a table-only document. - Separate markup from data extraction. This workflow saves each selected
<table>element with its markup. To collect cell values for JSON, CSV, or another data format, use Extract HTML Table Data .
HTML Table Extraction Documentation
See Extract HTML Tables in C# for table selection, row and cell processing, link extraction, and output examples. The Data Extraction section covers additional DOM navigation, CSS selector, XPath, and web content extraction workflows.
Get Started with Aspose.HTML for .NET API
Install Aspose.HTML for .NET from NuGet with the .NET CLI:
dotnet add package Aspose.HTML
You can also use the NuGet Package Manager UI or run Install-Package Aspose.HTML in the Package Manager Console. See the
installation guide
for setup details, or request a
temporary license
to evaluate the API without evaluation limitations.
Extract HTML Tables FAQ
1. Can I extract all tables from one HTML file?
Yes. QuerySelectorAll("table") returns every matching table in document order. Iterate through the result and assign a unique output name when each table must be saved separately.
2. How do I extract only one specific HTML table?
Use QuerySelector() with a stable ID, class, or structural selector, such as #price-table or main table. It returns the first matching table instead of collecting every <table> element.
3. Does an extracted HTML table retain its CSS formatting?
Inline styles and style attributes inside the table are retained in OuterHTML. CSS rules from the original document’s <head> or an external stylesheet must be copied or linked separately if the standalone file should keep the same appearance.
4. Can I extract table data to CSV or JSON instead of saving HTML markup?
Yes, but that is a different workflow. Iterate through the table rows, read the <th> and <td> values, and pass them to a CSV writer or JSON serializer. See
Extract HTML Table Data
for a complete example.
5. Are nested HTML tables extracted separately?
A broad table selector matches both outer and nested tables. Use a more specific selector when nested tables should remain only inside their parent table and should not also be written as separate output files.
Related HTML Table Workflows
Create, edit, or extract table values with other focused Aspose.HTML for .NET guides.