{"record":{"id":"f2c284e6200e977d","repo":"dotnet/wpf","slug":"reachpackaging-opendocorelementalreadycalled","errorCode":null,"errorMessage":"ReachPackaging_OpenDocOrElementAlreadyCalled","messagePattern":"ReachPackaging_OpenDocOrElementAlreadyCalled","errorType":"exception","errorClass":"XpsPackagingException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/PartEditor.cs","lineNumber":228,"sourceCode":"                return _xmlReader;\n            }\n        }\n\n        #endregion Internal properties\n\n        #region Internal methods\n\n        /// <summary>\n        /// Setup for writing\n        /// </summary>\n        internal\n        void\n        OpenDocumentForRead(\n            )\n        {\n            if (_xmlReader != null)\n            {\n                throw new XpsPackagingException(SR.ReachPackaging_OpenDocOrElementAlreadyCalled);\n            }\n\n            Stream stream = MetroPart.GetStream(FileMode.Open);\n  \n            _xmlReader = new XmlTextReader(stream);\n        }\n        \n        /// <summary>\n        /// Setup for writing\n        /// </summary>\n        internal\n        void\n        OpenDocumentForWrite(\n            )\n        {\n            if (_xmlWriter != null)\n            {\n                throw new XpsPackagingException(SR.ReachPackaging_OpenDocOrElementAlreadyCalled);","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/PartEditor.cs#L210-L246","documentation":"OpenDocumentForRead throws XpsPackagingException with resource ReachPackaging_OpenDocOrElementAlreadyCalled when an XPS part's XML reader is already open. Each PartEditor may open its document (or current element) for reading only once per reader lifetime; a second call indicates a sequencing bug in the packaging layer.","triggerScenarios":"Calling OpenDocumentForRead (or OpenElementForRead) twice on the same PartEditor without closing the previous reader; interleaved read/write operations on the same XpsDocument part; re-reading a part after a prior read left _xmlReader non-null.","commonSituations":"Custom XPS manipulation code iterating package parts and reopening them; writing an XPS document and then attempting to read a part already streamed; race between threads sharing a PartEditor instance.","solutions":["Call the corresponding Close method (closing the XmlReader) before reopening the document for read","Restructure code so each part is read exactly once per editor lifetime","Use separate PartEditor/XpsDocument instances for concurrent or repeated access","Guard reads with a null check on the reader or wrap in try/finally to ensure cleanup"],"exampleFix":"// before\neditor.OpenDocumentForRead();\neditor.OpenDocumentForRead(); // throws\n// after\neditor.OpenDocumentForRead();\n// ... read ...\neditor.CloseDocumentForRead();\neditor.OpenDocumentForRead();","handlingStrategy":"validation","validationCode":"if (editorHasOpenReader) { editor.CloseDocumentForRead(); }\neditor.OpenDocumentForRead();","typeGuard":null,"tryCatchPattern":"try { editor.OpenDocumentForRead(); } catch (XpsPackagingException) { editor.CloseDocumentForRead(); editor.OpenDocumentForRead(); }","preventionTips":["Pair every Open*ForRead call with its Close in a try/finally","Never reopen a part while its reader is still open","Use one PartEditor per stream/thread; do not share instances","Structure XPS writing so reads happen after writes are committed to a fresh editor"],"tags":["xps","invalid-state","packaging","wpf"],"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-21T21:30:21.729Z"}