{"record":{"id":"f02951b8771b8661","repo":"dotnet/wpf","slug":"fixeddocument-has-more-than-one-related-document-structure","errorCode":null,"errorMessage":"FixedDocument has more than one related document structure.","messagePattern":"FixedDocument has more than one related document structure\\.","errorType":"exception","errorClass":"XpsPackagingException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsFixedDocumentReaderWriter.cs","lineNumber":475,"sourceCode":"            _thumbnail = CurrentXpsManager.AddThumbnail( imageType, this, Thumbnail );\n            _metroPart.CreateRelationship( _thumbnail.Uri,\n                                           TargetMode.Internal,\n                                           XpsS0Markup.ThumbnailRelationshipName\n                                          );\n           return _thumbnail;\n        }\n\n        /// <summary>\n        /// </summary>\n        public\n        XpsStructure\n        AddDocumentStructure(\n            )\n        {\n            if (this.DocumentStructure != null)\n            {\n                // Document structure already available for this FixedDocument\n                throw new XpsPackagingException(SR.ReachPackaging_MoreThanOneDocStructure);\n            }\n\n            Uri pageUri = this.CurrentXpsManager.CreateStructureUri();\n            //\n            // Create the part and writer\n            //\n            PackagePart metroPart = this.CurrentXpsManager.GeneratePart(\n                        XpsS0Markup.DocumentStructureContentType,\n                        pageUri);\n\n            _documentStructure = new XpsStructure(CurrentXpsManager, this, metroPart);\n\n            //\n            // Create the relationship between the document and the document-structure\n            // Not in INode.Flush because IXpsFixedDocumentReader has no commit.\n            //\n\n            string structurePath = XpsManager.MakeRelativePath(this.Uri, _documentStructure.Uri);","sourceCodeStart":457,"sourceCodeEnd":493,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsFixedDocumentReaderWriter.cs#L457-L493","documentation":"XpsFixedDocumentReaderWriter.AddDocumentStructure throws XpsPackagingException (ReachPackaging_MoreThanOneDocStructure) when the FixedDocument already has a document structure part. The XPS spec permits at most one DocumentStructure relationship per FixedDocument. This is an eager check via the DocumentStructure property before creating a new structure part.","triggerScenarios":"Calling AddDocumentStructure a second time on the same XpsFixedDocumentReaderWriter after a document structure part was already added (DocumentStructure property is non-null).","commonSituations":"Retry logic that re-invokes AddDocumentStructure after a partial failure; refactors that call it both in setup and in a write loop; accidental duplicate processing of the same document.","solutions":["Check the DocumentStructure property before calling AddDocumentStructure and skip if already non-null.","Track in application state whether the structure part was created for this document.","Rebuild the package if you truly need a new document structure; you cannot add a second one."],"exampleFix":"// before\ndoc.AddDocumentStructure();\ndoc.AddDocumentStructure(); // throws\n// after\nif (doc.DocumentStructure == null)\n    doc.AddDocumentStructure();","handlingStrategy":"validation","validationCode":"if (doc.DocumentStructure == null)\n    doc.AddDocumentStructure();","typeGuard":null,"tryCatchPattern":"try { doc.AddDocumentStructure(); }\ncatch (XpsPackagingException) { /* structure already exists; reuse it */ }","preventionTips":["Guard every AddDocumentStructure call with a null check on DocumentStructure.","Make structure creation idempotent in helper methods.","Avoid retry wrappers that can re-run the creation step."],"tags":["wpf","xps","duplicate","document-structure"],"backgroundTag":"file-already-exists","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}