Working with XML Processing

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:

MapperXML partResponsibility
WorkbookXmlMapperxl/workbook.xmlWorkbook-level metadata, sheet references, defined names
WorksheetXmlMapperxl/worksheets/sheetN.xmlCell data, formulas, hyperlinks, conditional formats, data validation
SharedStringTableXmlMapperxl/sharedStrings.xmlShared string table for de-duplicated string values
StylesheetXmlMapperxl/styles.xmlCell 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 = true in LoadOptions to enable the mapper’s fault-tolerant parsing path.
  • Catch XmlParsingException in addition to WorkbookLoadException when 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 Workbook construction and Save().
  • 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

IssueCauseFix
XmlParsingException on loadMalformed XML in one of the XLSX partsSet TryRepairXml = true in LoadOptions
Missing styles after round-tripStylesheetXmlMapper could not parse a custom style recordCheck LoadDiagnostics.Issues for style-related repair entries
Shared strings missing in loaded fileSharedStringTableXmlMapper encountered a corrupt shared string tableEnable 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

ClassDescription
WorkbookXmlMapperReads/writes xl/workbook.xml
WorksheetXmlMapperReads/writes per-sheet XML parts
SharedStringTableXmlMapperReads/writes xl/sharedStrings.xml
StylesheetXmlMapperReads/writes xl/styles.xml
XmlParsingExceptionThrown on unrecoverable XML parse failures