{"record":{"id":"fd1d5c5724209822","repo":"dotnet/wpf","slug":"sr-reachserialization-fixeddocumentindocument-fd1d5c","errorCode":null,"errorMessage":"SR.ReachSerialization_FixedDocumentInDocument","messagePattern":"SR\\.ReachSerialization_FixedDocumentInDocument","errorType":"exception","errorClass":"XpsSerializationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/XpsSerializationManager.cs","lineNumber":840,"sourceCode":"        /// </param>\n        internal\n        override\n        void\n        AddRelationshipToCurrentPage(\n            Uri targetUri,\n            string relationshipName\n            )\n        {\n            _packagingPolicy?.RelateResourceToCurrentPage(targetUri, relationshipName);\n        }\n\n        internal\n        void\n        RegisterDocumentStart()\n        {\n            if( _documentStartState )\n            {\n                throw new XpsSerializationException(SR.ReachSerialization_FixedDocumentInDocument);\n            }\n            if( _pageStartState)\n            {\n                throw new XpsSerializationException(SR.ReachSerialization_FixedDocumentInPage);\n            }\n            //\n            // Entering Document  Started state\n            //\n            _documentStartState = true;\n            //\n            // Increment the number of documents serialized\n            //\n            _documentNumber += 1;\n            //\n            // Clearing the number of pages for this document\n            //\n            _pageNumber = 0;\n        }","sourceCodeStart":822,"sourceCodeEnd":858,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/XpsSerializationManager.cs#L822-L858","documentation":"RegisterDocumentStart maintains the XPS document state machine: a FixedDocument cannot begin while another document is already open (_documentStartState). If RegisterDocumentStart is called twice without an intervening RegisterDocumentEnd, XpsSerializationException(SR.ReachSerialization_FixedDocumentInDocument) is thrown — nested FixedDocuments are illegal in XPS.","triggerScenarios":"Calling RegisterDocumentStart twice without RegisterDocumentEnd in between — typically from a custom serializer that writes a second <FixedDocument> before closing the first, or from duplicate event/serialization callbacks.","commonSituations":"Hand-rolled XPS writers built on IXpsSerializationManager; pagination code that restarts a document per page instead of using RegisterPageStart; re-entrant serialization triggered by print events.","solutions":["Call RegisterDocumentEnd before starting the next document — strictly alternate document start/end.","Nest pages correctly: within one document use RegisterPageStart/RegisterPageEnd, not another document start.","Guard your serializer with a flag so RegisterDocumentStart is invoked once per FixedDocument.","Prefer higher-level APIs (XpsDocumentWriter with DocumentPaginator) that manage the state machine for you."],"exampleFix":"// before\nmanager.RegisterDocumentStart();\nmanager.RegisterDocumentStart(); // nested document\n// after\nmanager.RegisterDocumentStart();\n// ... write pages ...\nmanager.RegisterDocumentEnd();\nmanager.RegisterDocumentStart(); // next document","handlingStrategy":"validation","validationCode":"if (documentOpen)\n    throw new InvalidOperationException(\"RegisterDocumentStart called while a FixedDocument is already open; call RegisterDocumentEnd first.\");","typeGuard":null,"tryCatchPattern":"try\n{\n    manager.RegisterDocumentStart();\n}\ncatch (System.Windows.Xps.XpsSerializationException ex) when (ex.Message.Contains(\"document\"))\n{\n    // recover: end the current document, then restart\n    manager.RegisterDocumentEnd();\n    manager.RegisterDocumentStart();\n}","preventionTips":["Alternate RegisterDocumentStart/RegisterDocumentEnd strictly, one pair per FixedDocument.","Track document state with your own boolean as a pre-check.","Prefer DocumentPaginator + XpsDocumentWriter so the framework drives the state machine."],"tags":["wpf","xps","state-machine","serialization"],"backgroundTag":"invalid-state-transition","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"}