{"record":{"id":"8bf884ce23c8657c","repo":"dotnet/wpf","slug":"sr-closexamlwriterbeforereading","errorCode":null,"errorMessage":"SR.CloseXamlWriterBeforeReading","messagePattern":"SR\\.CloseXamlWriterBeforeReading","errorType":"exception","errorClass":"XamlException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlNodeList.cs","lineNumber":56,"sourceCode":"            }\n            else\n            {\n                _nodeList = new List<XamlNode>(size);\n            }\n\n            _writer = new WriterDelegate(Add, AddLineInfo, schemaContext);\n        }\n\n        public XamlWriter Writer\n        {\n            get { return _writer; }\n        }\n\n        public XamlReader GetReader()\n        {\n            if (!_readMode)\n            {\n                throw new XamlException(SR.CloseXamlWriterBeforeReading);\n            }\n\n            if (_writer.SchemaContext is null)\n            {\n                throw new XamlException(SR.SchemaContextNotInitialized);\n            }\n\n            return new ReaderMultiIndexDelegate(_writer.SchemaContext, Index, _nodeList.Count, _hasLineInfo);\n        }\n\n        private void Add(XamlNodeType nodeType, object data)\n        {\n            if (!_readMode)\n            {\n                if (nodeType != XamlNodeType.None)\n                {\n                    XamlNode node = new XamlNode(nodeType, data);\n                    _nodeList.Add(node);","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlNodeList.cs#L38-L74","documentation":"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.","triggerScenarios":"Calling xamlNodeList.GetReader() while the XamlNodeList's writer is still open (EndObject/EOF not yet written, _readMode == false).","commonSituations":"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().","solutions":["Close/complete the writer (call Close or write the EOF node) before calling GetReader().","Restructure code so all nodes are written first, then read.","Check the _readMode flag via library API ordering rather than reading it mid-stream."],"exampleFix":"// before\nvar list = new XamlNodeList(schemaContext);\nvar writer = list.Writer;\nWriteNodes(writer);\nvar reader = list.GetReader(); // too early\n// after\nvar list = new XamlNodeList(schemaContext);\nvar writer = list.Writer;\nWriteNodes(writer);\nwriter.Close();\nvar reader = list.GetReader();","handlingStrategy":"validation","validationCode":"if (!writer.IsClosed) writer.Close();\nvar reader = nodeList.GetReader();","typeGuard":null,"tryCatchPattern":"try { var reader = nodeList.GetReader(); }\ncatch (XamlException) { /* writer not closed yet — close and retry */ }","preventionTips":["Always call Close() on the XamlNodeList writer before reading.","Structure write-then-read phases explicitly in buffering pipelines."],"tags":["xaml","invalid-state","xamlwriter"],"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"}