iOfficeAI/OfficeCLI · error · InvalidDataException

Part '{target}' contains no root element (only an XML declar

Error message

Part '{target}' contains no root element (only an XML declaration, whitespace, or BOM).

What it means

Thrown by TryReadViaSdkPackage when an OpenXML part exists in the package but its content, after stripping the XML prolog/BOM/whitespace, is empty — i.e. there is no root element. This indicates a corrupt or truncated part. The exception is InvalidDataException (not swallowed): the caller is expected to treat presence-without-content as a data-integrity failure, not a missing part.

Source

Thrown at src/officecli/Core/RawXmlHelper.cs:1100

        try
        {
            var clean = StripUriSuffixes(partPath.AsSpan().Trim()).ToString();
            var target = clean.StartsWith('/') ? clean : "/" + clean;
            Uri uri;
            try { uri = new Uri(target, UriKind.Relative); } catch { return null; }

#pragma warning disable OOXML0001
            var pkg = package.GetPackage();
#pragma warning restore OOXML0001
            if (!pkg.PartExists(uri)) return null;
            var part = pkg.GetPart(uri);

            using var stream = part.GetStream(FileMode.Open, FileAccess.Read);
            using var reader = new StreamReader(stream);
            var content = reader.ReadToEnd();
            var stripped = StripXmlProlog(content);
            if (stripped.Length == 0)
                throw new InvalidDataException(
                    $"Part '{target}' contains no root element (only an XML " +
                    $"declaration, whitespace, or BOM).");
            return stripped;
        }
        catch (InvalidDataException) { throw; }
        catch
        {
            return null;
        }
    }

    public static OpenXmlPart? FindPartByZipUri(OpenXmlPackage package, string partPath)
    {
        // Trim surrounding whitespace, discard fragment/query, and normalize
        // leading slash. Fragments and query strings are not part of OPC
        // URIs; users may inadvertently type them and we should resolve
        // against the bare part path.
        partPath = StripUriSuffixes(partPath.AsSpan().Trim()).ToString();

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Open the .docx/.xlsx/.pptx as a zip and inspect the named part to confirm it is genuinely empty or malformed.
  2. Regenerate or repair the source document (re-save from Office, or rebuild the package with a proper root element).
  3. If reading optional parts, catch InvalidDataException and treat it as 'unreadable part' distinct from 'absent part'.
Defensive patterns

Strategy: try-catch

Validate before calling

// Pre-check a part has content before relying on it
var preview = RawXmlHelper.TryReadByZipUri(package, filePath, partPath);
// TryReadByZipUri returns null for absent parts but throws InvalidDataException for empty ones;
// catch to distinguish unreadable from missing.

Try / catch

try { var xml = RawXmlHelper.TryReadByZipUri(package, filePath, partPath); }
catch (InvalidDataException ex) { /* part exists but is corrupt/empty: log part path, halt data processing */ }

Prevention

When it happens

Trigger: Reading a part via its zip URI through the SDK's underlying IPackage when the part stream contains only an XML declaration, whitespace, or a BOM. Reached from TryReadByZipUri after the typed-part lookup misses (e.g. a .rels part or non-typed XML part).

Common situations: A document was produced by a tool that wrote a part stub but never serialized its body, or a file was truncated mid-write, or a custom XML part is malformed. Streaming a partially-flushed package also triggers it.

Related errors


AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13). Data as JSON: /api/errors/000c398ece32d9ba. Report an issue: GitHub.