{"record":{"id":"000c398ece32d9ba","repo":"iOfficeAI/OfficeCLI","slug":"part-target-contains-no-root-element-only-an","errorCode":null,"errorMessage":"Part '{target}' contains no root element (only an XML declaration, whitespace, or BOM).","messagePattern":"Part '(.+?)' contains no root element \\(only an XML declaration, whitespace, or BOM\\)\\.","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"src/officecli/Core/RawXmlHelper.cs","lineNumber":1100,"sourceCode":"        try\n        {\n            var clean = StripUriSuffixes(partPath.AsSpan().Trim()).ToString();\n            var target = clean.StartsWith('/') ? clean : \"/\" + clean;\n            Uri uri;\n            try { uri = new Uri(target, UriKind.Relative); } catch { return null; }\n\n#pragma warning disable OOXML0001\n            var pkg = package.GetPackage();\n#pragma warning restore OOXML0001\n            if (!pkg.PartExists(uri)) return null;\n            var part = pkg.GetPart(uri);\n\n            using var stream = part.GetStream(FileMode.Open, FileAccess.Read);\n            using var reader = new StreamReader(stream);\n            var content = reader.ReadToEnd();\n            var stripped = StripXmlProlog(content);\n            if (stripped.Length == 0)\n                throw new InvalidDataException(\n                    $\"Part '{target}' contains no root element (only an XML \" +\n                    $\"declaration, whitespace, or BOM).\");\n            return stripped;\n        }\n        catch (InvalidDataException) { throw; }\n        catch\n        {\n            return null;\n        }\n    }\n\n    public static OpenXmlPart? FindPartByZipUri(OpenXmlPackage package, string partPath)\n    {\n        // Trim surrounding whitespace, discard fragment/query, and normalize\n        // leading slash. Fragments and query strings are not part of OPC\n        // URIs; users may inadvertently type them and we should resolve\n        // against the bare part path.\n        partPath = StripUriSuffixes(partPath.AsSpan().Trim()).ToString();","sourceCodeStart":1082,"sourceCodeEnd":1118,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/Core/RawXmlHelper.cs#L1082-L1118","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Open the .docx/.xlsx/.pptx as a zip and inspect the named part to confirm it is genuinely empty or malformed.","Regenerate or repair the source document (re-save from Office, or rebuild the package with a proper root element).","If reading optional parts, catch InvalidDataException and treat it as 'unreadable part' distinct from 'absent part'."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Pre-check a part has content before relying on it\nvar preview = RawXmlHelper.TryReadByZipUri(package, filePath, partPath);\n// TryReadByZipUri returns null for absent parts but throws InvalidDataException for empty ones;\n// catch to distinguish unreadable from missing.","typeGuard":null,"tryCatchPattern":"try { var xml = RawXmlHelper.TryReadByZipUri(package, filePath, partPath); }\ncatch (InvalidDataException ex) { /* part exists but is corrupt/empty: log part path, halt data processing */ }","preventionTips":["Treat a present-but-empty part as corruption, not absence.","Validate packages with a zip inspector before batch processing.","Never swallow InvalidDataException from raw read paths — it signals integrity loss."],"tags":["raw-xml","ooxml","corrupt-package","invalid-data"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}