{"record":{"id":"9445131999b07193","repo":"dotnet/wpf","slug":"sr-bamlscopeerror-0-1","errorCode":null,"errorMessage":"SR.BamlScopeError: {0} {1}","messagePattern":"SR\\.BamlScopeError: (.+?) (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlReader.cs","lineNumber":1851,"sourceCode":"        }\n\n        /***************************************************************************\\\n        *\n        * BamlReader.ReadDocumentEndRecord\n        *\n        * Read the end of the document record.  This is used to flag that the end\n        * of the file has been reached.\n        *\n        \\***************************************************************************/\n\n        private void ReadDocumentEndRecord()\n        {\n            // Pop information off the node stack to ensure we have matched all the\n            // start and end nodes and have nothing left but the start document node.\n            BamlNodeInfo nodeInfo = _nodeStack.Pop();\n            if (nodeInfo.RecordType != BamlRecordType.DocumentStart)\n            {\n                throw new InvalidOperationException(SR.Format(SR.BamlScopeError,\n                                                    nodeInfo.RecordType.ToString(),\n                                                    \"DocumentEnd\"));\n            }\n\n            ClearProperties();\n            NodeTypeInternal = BamlNodeType.EndDocument;\n        }\n\n        private void ReadAssemblyInfoRecord()\n        {\n            BamlAssemblyInfoRecord asmRecord = (BamlAssemblyInfoRecord)_currentBamlRecord;\n            MapTable.LoadAssemblyInfoRecord(asmRecord);\n\n            Assembly asm = Assembly.Load(asmRecord.AssemblyFullName);\n            foreach (XmlnsDefinitionAttribute xmlnsDef in asm.GetCustomAttributes(typeof(XmlnsDefinitionAttribute), true))\n            {\n                SetXmlNamespace(xmlnsDef.ClrNamespace, asm.FullName, xmlnsDef.XmlNamespace);\n            }","sourceCodeStart":1833,"sourceCodeEnd":1869,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlReader.cs#L1833-L1869","documentation":"BamlReader.ReadDocumentEndRecord pops the node stack expecting the matching DocumentStart record. If the popped node is anything else, the BAML stream's start/end records are unbalanced and the reader throws InvalidOperationException with SR.BamlScopeError, naming the unexpected record type and \"DocumentEnd\".","triggerScenarios":"Calling ReadDocumentEndRecord (via Read when the stream yields a DocumentEnd record) while the top of _nodeStack is not BamlRecordType.DocumentStart — e.g. elements or properties were opened but never closed before the document end record.","commonSituations":"Corrupted or truncated BAML resource; a BAML stream assembled by custom tooling with mismatched start/end records; interrupting a read loop so records are skipped and the stack gets out of sync.","solutions":["Regenerate the BAML by rebuilding the project — the stream is likely corrupt or hand-modified.","Ensure every Read call sequence consumes all records (do not abandon reads mid-stream and reuse the reader).","If generating BAML yourself, verify every DocumentStart/ElementStart record has its matching end record in order."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// verify balanced document scope by walking records before reading\nint depth = 0;\nforeach (var r in records) {\n    if (r.RecordType == BamlRecordType.DocumentStart) depth++;\n    if (r.RecordType == BamlRecordType.DocumentEnd && --depth != 0)\n        throw new InvalidOperationException(\"Unbalanced DocumentStart/DocumentEnd in BAML.\");\n}","typeGuard":"bool DocumentScopeBalanced(IEnumerable<BamlRecord> recs) => recs.Count(r => r.RecordType == BamlRecordType.DocumentStart) == recs.Count(r => r.RecordType == BamlRecordType.DocumentEnd);","tryCatchPattern":"try { reader.Read(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"DocumentEnd\")) {\n    log.LogError(\"Unbalanced BAML document scope — regenerate the BAML resource.\");\n}","preventionTips":["Read BAML streams to completion; never abandon a reader mid-stream and reuse it.","Rebuild after any manual resource manipulation.","Hash-verify deployed assemblies to catch corrupted resources."],"tags":["wpf","baml","corrupt-stream","scope-mismatch"],"backgroundTag":"schema-validation-failed","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"}