{"record":{"id":"a467cde55771b166","repo":"dotnet/wpf","slug":"sr-cannotwriteclosedwriter","errorCode":null,"errorMessage":"SR.CannotWriteClosedWriter","messagePattern":"SR\\.CannotWriteClosedWriter","errorType":"exception","errorClass":"XamlException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlNodeList.cs","lineNumber":83,"sourceCode":"        }\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);\n                    return;\n                }\n\n                Debug.Assert(XamlNode.IsEof_Helper(nodeType, data));\n                _readMode = true;\n            }\n            else\n            {\n                throw new XamlException(SR.CannotWriteClosedWriter);\n            }\n        }\n\n        private void AddLineInfo(int lineNumber, int linePosition)\n        {\n            if (_readMode)\n            {\n                throw new XamlException(SR.CannotWriteClosedWriter);\n            }\n\n            XamlNode node = new XamlNode(new LineInfo(lineNumber, linePosition));\n            _nodeList.Add(node);\n            if (!_hasLineInfo)\n            {\n                _hasLineInfo = true;\n            }\n        }\n","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlNodeList.cs#L65-L101","documentation":"XamlNodeList.Add throws XamlException(SR.CannotWriteClosedWriter) when new nodes arrive after the writer has been closed and the list switched to read mode (_readMode == true). Once closed, the node list is immutable.","triggerScenarios":"Calling any of the writer's Write* methods (via XamlNodeList.Writer) after Close/EOF was written, or calling Add indirectly after read mode was entered.","commonSituations":"Reusing a buffered XamlNodeList for a second write pass, or event/callback-driven writers that keep firing after the pipeline finished.","solutions":["Create a new XamlNodeList for additional writes.","Ensure no code paths write after Close() — guard writes with an isClosed flag.","Restructure so the full node stream is composed before closing."],"exampleFix":"// before\nwriter.Close();\nwriter.WriteStartObject(xamlType); // throws\n// after\nwriter.Close();\nvar newList = new XamlNodeList(schemaContext);\nnewList.Writer.WriteStartObject(xamlType);","handlingStrategy":"validation","validationCode":"if (!list.Writer.IsClosed) { /* safe to write */ }","typeGuard":null,"tryCatchPattern":"try { writer.WriteEndObject(); }\ncatch (XamlException) { /* writer closed — create a new XamlNodeList */ }","preventionTips":["Treat a closed XamlNodeList as immutable; allocate a new one for more writes.","Guard callback-driven writes with a disposed/closed flag."],"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"}