Edit Existing HTML Tables with a .NET API

Aspose.HTML for .NET loads an existing HTML file into an HTMLDocument . You can locate a table with a CSS selector, update its cells and styles through the DOM, add or remove rows, and save the complete document without rebuilding its unrelated content.


Edit a Table in an HTML File with C#

The following example opens input.html, selects its first table, changes the first data cell, appends a row, updates table CSS, and saves the result as edited-table.html.

C# Code to Edit an HTML Table

using Aspose.Html;
using System;

// Load the HTML document that contains the table
using HTMLDocument document = new HTMLDocument("input.html");

// Select the first table and verify that it exists
HTMLTableElement table = (HTMLTableElement)document.QuerySelector("table");
if (table == null)
{
    throw new InvalidOperationException("The HTML document does not contain a table.");
}

// Update the first data cell when one is available
HTMLTableCellElement firstCell = (HTMLTableCellElement)table.QuerySelector("td");
if (firstCell != null)
{
    firstCell.TextContent = "Updated value";
}

// Append a row and populate its cells
HTMLTableRowElement newRow = (HTMLTableRowElement)table.InsertRow(table.Rows.Length);
string[] values = { "New item", "Ready" };
foreach (string value in values)
{
    HTMLTableCellElement cell = (HTMLTableCellElement)newRow.InsertCell(newRow.Cells.Length);
    cell.TextContent = value;
}

// Apply CSS without replacing the table's other attributes
table.Style.SetProperty("border", "1px solid #808080");
table.Style.SetProperty("border-collapse", "collapse");
table.Style.SetProperty("width", "100%");

// Save the complete modified document
document.Save("edited-table.html");

Steps to Edit an HTML Table

  1. Open the source file with an HTMLDocument constructor.
  2. Locate the required table with QuerySelector() . Use an ID or class selector instead of table when the document contains several tables.
  3. Select an existing cell and update its TextContent without replacing the rest of the table markup.
  4. Append a row with InsertRow() and create its cells with InsertCell() .
  5. Change presentation through the table’s Style declaration and call Save() to write the complete edited document.

HTML Table Editing Options

  • Target the correct table. Use selectors such as #orders, .price-table, or main table when editing a document that contains more than one table.
  • Modify existing content. Change cell text, attributes, classes, and inline CSS without recreating the table or replacing unrelated HTML.
  • Change table structure. Insert or delete rows and cells through the table DOM interfaces while preserving their document order.
  • Apply modern presentation rules. Use CSS properties for borders, width, alignment, and spacing instead of obsolete presentational HTML attributes.
  • Preserve the surrounding document. Saving the loaded HTMLDocument retains content outside the edited table. For a workflow that isolates selected <table> elements with their markup, see Extract HTML Tables .

HTML Table Editing Documentation

See Edit an HTML Document for examples of finding, inserting, changing, and removing DOM nodes. The HTML DOM documentation explains document navigation and the element model used to modify HTML content programmatically.


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.


Edit HTML Tables FAQ

1. How do I edit a specific HTML table when a page contains several tables?

Give the target table a stable ID or class and pass a matching CSS selector, such as [id="orders"] or .price-table, to QuerySelector(). A generic table selector returns only the first table in the document.

2. Can I change a table cell without replacing the complete table?

Yes. Select the required <td> or <th> element and assign its TextContent for plain text. When the cell must contain nested markup, update its DOM children or use InnerHTML with trusted content.

3. How do I add or remove rows from an HTML table in C#?

Call InsertRow() with the required row index, then add cells with InsertCell(). Use DeleteRow() to remove a row; validate the index against the table’s Rows.Length before changing the structure.

4. Should table formatting be changed with HTML attributes or CSS?

Use CSS for borders, width, alignment, spacing, colors, and other presentation. Attributes such as align, border, and cellspacing belong to the older presentational HTML model and should not be introduced into modern markup.

5. Does saving an edited table preserve the rest of the HTML document?

Yes. When you load the source into an HTMLDocument, modify only the selected table nodes, and save that document, unrelated elements remain in the output. Deliberately replacing a parent element or its InnerHTML can also replace its descendants.

Related HTML Table Workflows

Create tables, extract complete markup, or collect cell values with other focused Aspose.HTML for .NET guides.