dotnet/wpf · error · XpsPackagingException

ReachPackaging_OpenDocOrElementAlreadyCalled

Error message

ReachPackaging_OpenDocOrElementAlreadyCalled

What it means

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.

Solutions

  1. Call the corresponding Close method (closing the XmlReader) before reopening the document for read
  2. Restructure code so each part is read exactly once per editor lifetime
  3. Use separate PartEditor/XpsDocument instances for concurrent or repeated access
  4. Guard reads with a null check on the reader or wrap in try/finally to ensure cleanup

Example fix

// before
editor.OpenDocumentForRead();
editor.OpenDocumentForRead(); // throws
// after
editor.OpenDocumentForRead();
// ... read ...
editor.CloseDocumentForRead();
editor.OpenDocumentForRead();
Defensive patterns

Strategy: validation

Validate before calling

if (editorHasOpenReader) { editor.CloseDocumentForRead(); }
editor.OpenDocumentForRead();

Try / catch

try { editor.OpenDocumentForRead(); } catch (XpsPackagingException) { editor.CloseDocumentForRead(); editor.OpenDocumentForRead(); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/f2c284e6200e977d. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/PartEditor.cs:228

                return _xmlReader;
            }
        }

        #endregion Internal properties

        #region Internal methods

        /// <summary>
        /// Setup for writing
        /// </summary>
        internal
        void
        OpenDocumentForRead(
            )
        {
            if (_xmlReader != null)
            {
                throw new XpsPackagingException(SR.ReachPackaging_OpenDocOrElementAlreadyCalled);
            }

            Stream stream = MetroPart.GetStream(FileMode.Open);
  
            _xmlReader = new XmlTextReader(stream);
        }
        
        /// <summary>
        /// Setup for writing
        /// </summary>
        internal
        void
        OpenDocumentForWrite(
            )
        {
            if (_xmlWriter != null)
            {
                throw new XpsPackagingException(SR.ReachPackaging_OpenDocOrElementAlreadyCalled);

View on GitHub (pinned to 81131a70a4)