dotnet/wpf · error · XamlException

SR.CloseXamlWriterBeforeReading

Error message

SR.CloseXamlWriterBeforeReading

What it means

XamlNodeList.GetReader throws XamlException(SR.CloseXamlWriterBeforeReading) if GetReader is called before the associated XamlWriter has been closed, because the node list is still being written and the reader must see a complete node stream.

Solutions

  1. Close/complete the writer (call Close or write the EOF node) before calling GetReader().
  2. Restructure code so all nodes are written first, then read.
  3. Check the _readMode flag via library API ordering rather than reading it mid-stream.

Example fix

// before
var list = new XamlNodeList(schemaContext);
var writer = list.Writer;
WriteNodes(writer);
var reader = list.GetReader(); // too early
// after
var list = new XamlNodeList(schemaContext);
var writer = list.Writer;
WriteNodes(writer);
writer.Close();
var reader = list.GetReader();
Defensive patterns

Strategy: validation

Validate before calling

if (!writer.IsClosed) writer.Close();
var reader = nodeList.GetReader();

Try / catch

try { var reader = nodeList.GetReader(); }
catch (XamlException) { /* writer not closed yet — close and retry */ }

Prevention

When it happens

Trigger: Calling xamlNodeList.GetReader() while the XamlNodeList's writer is still open (EndObject/EOF not yet written, _readMode == false).

Common situations: Buffering XAML into a XamlNodeList and trying to re-read it before finishing the write pipeline, e.g. calling GetReader mid-write instead of after writer.Close().

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/8bf884ce23c8657c. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlNodeList.cs:56

            }
            else
            {
                _nodeList = new List<XamlNode>(size);
            }

            _writer = new WriterDelegate(Add, AddLineInfo, schemaContext);
        }

        public XamlWriter Writer
        {
            get { return _writer; }
        }

        public XamlReader GetReader()
        {
            if (!_readMode)
            {
                throw new XamlException(SR.CloseXamlWriterBeforeReading);
            }

            if (_writer.SchemaContext is null)
            {
                throw new XamlException(SR.SchemaContextNotInitialized);
            }

            return new ReaderMultiIndexDelegate(_writer.SchemaContext, Index, _nodeList.Count, _hasLineInfo);
        }

        private void Add(XamlNodeType nodeType, object data)
        {
            if (!_readMode)
            {
                if (nodeType != XamlNodeType.None)
                {
                    XamlNode node = new XamlNode(nodeType, data);
                    _nodeList.Add(node);

View on GitHub (pinned to 81131a70a4)