Working with XML Processing
Working with XML Processing
XLSX files store their data in XML parts inside an OPC package. Aspose.Cells FOSS for .NET exposes the XML parsing and serialization layer through four mapper classes: WorkbookXmlMapper, WorksheetXmlMapper, SharedStringTableXmlMapper, and StylesheetXmlMapper. Each mapper implements both Read and Write operations for its corresponding XML part. These are internal infrastructure APIs — most application code works entirely through the Workbook and Worksheet objects.
XML Mapper Classes
Each XML mapper is responsible for one XML part of the XLSX structure:
| Mapper | XML part | Responsibility |
|---|---|---|
WorkbookXmlMapper | xl/workbook.xml | Workbook-level metadata, sheet references, defined names |
WorksheetXmlMapper | xl/worksheets/sheetN.xml | Cell data, formulas, hyperlinks, conditional formats, data validation |
SharedStringTableXmlMapper | xl/sharedStrings.xml | Shared string table for de-duplicated string values |
StylesheetXmlMapper | xl/styles.xml | Cell styles, fonts, fills, borders, number formats |
Each mapper exposes two methods — Read and Write — invoked by the library’s load and save pipelines respectively.
XmlParsingException
XmlParsingException is thrown when a mapper encounters malformed XML that cannot be repaired. It is raised before WorkbookLoadException in the error hierarchy when the failure occurs during XML parsing specifically.
using Aspose.Cells_FOSS;
var opts = new LoadOptions
{
TryRepairPackage = true,
TryRepairXml = true,
};
try
{
var wb = new Workbook("malformed.xlsx", opts);
Console.WriteLine("Loaded: " + wb.Worksheets.Count + " sheet(s)");
}
catch (XmlParsingException ex)
{
Console.WriteLine("XML parse error: " + ex.Message);
}
catch (WorkbookLoadException ex)
{
Console.WriteLine("Load error: " + ex.Message);
}Tips and Best Practices
- Set
TryRepairXml = trueinLoadOptionsto enable the mapper’s fault-tolerant parsing path. - Catch
XmlParsingExceptionin addition toWorkbookLoadExceptionwhen handling files from external systems that may produce malformed XML. - The XML mapper classes are not intended to be instantiated by application code — they are invoked automatically during
Workbookconstruction andSave(). - If you need to inspect raw XML content for diagnostics, extract the XLSX as a ZIP and examine the relevant XML parts directly.
Common Issues
| Issue | Cause | Fix |
|---|---|---|
XmlParsingException on load | Malformed XML in one of the XLSX parts | Set TryRepairXml = true in LoadOptions |
| Missing styles after round-trip | StylesheetXmlMapper could not parse a custom style record | Check LoadDiagnostics.Issues for style-related repair entries |
| Shared strings missing in loaded file | SharedStringTableXmlMapper encountered a corrupt shared string table | Enable TryRepairXml; verify original file has a valid xl/sharedStrings.xml |
FAQ
Can I implement my own XML mapper?
The XML mapper classes are sealed internal infrastructure. Custom mappers are not supported in the current API surface.
Why is there a separate SharedStringTableXmlMapper?
The OOXML specification separates string values into a shared string table (xl/sharedStrings.xml) to de-duplicate repeated strings across the workbook. The mapper handles reading and writing this table independently.
Does TryRepairXml = true fix all XML problems?
TryRepairXml handles recoverable problems such as unclosed elements, missing namespaces, and truncated values. Structurally valid but semantically inconsistent XML (e.g. broken formula references) may still cause XmlParsingException.
API Reference Summary
| Class | Description |
|---|---|
WorkbookXmlMapper | Reads/writes xl/workbook.xml |
WorksheetXmlMapper | Reads/writes per-sheet XML parts |
SharedStringTableXmlMapper | Reads/writes xl/sharedStrings.xml |
StylesheetXmlMapper | Reads/writes xl/styles.xml |
XmlParsingException | Thrown on unrecoverable XML parse failures |