{"record":{"id":"0bdfac328e81c8c3","repo":"dotnet/wpf","slug":"sr-parserdictionarysealed-the-dictionary-is-sealed-and","errorCode":null,"errorMessage":"SR.ParserDictionarySealed (the dictionary is sealed and cannot be modified)","messagePattern":"SR\\.ParserDictionarySealed \\(the dictionary is sealed and cannot be modified\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlnsDictionary.cs","lineNumber":594,"sourceCode":"\n#region Private       \n        private void Initialize()\n        {\n            // We set the initial array to 8 and double from there when we run of the space. \n            // For the start case, we set the DefaultNamespaceuri to null. \n            _nsDeclarations = new NamespaceDeclaration[8];\n            _nsDeclarations[0].Prefix = string.Empty;\n            _nsDeclarations[0].Uri = null;\n            _nsDeclarations[0].ScopeCount = 0;\n            _lastDecl = 0;\n            _countDecl = 0;\n       }\n\n       private void CheckSealed()\n        {\n            if (IsReadOnly)\n            {\n                throw new InvalidOperationException(SR.ParserDictionarySealed);\n            }\n        }\n\n        /// <summary>\n        /// Helper to Add Namespace - Checks for prefix from rear.\n        ///  If exists : override it locally, if not Adds an entry.\n        /// </summary>\n        /// <param name=\"prefix\">prefix to add</param>\n        /// <param name=\"xmlNamespace\">namespace uri string to add</param>\n        private void AddNamespace(string prefix, string xmlNamespace)\n        {\n            CheckSealed();\n            \n            if (xmlNamespace == null)\n                throw new ArgumentNullException(nameof(xmlNamespace));\n\n            if (prefix == null)\n                throw new ArgumentNullException(nameof(prefix));","sourceCodeStart":576,"sourceCodeEnd":612,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlnsDictionary.cs#L576-L612","documentation":"XmlnsDictionary.CheckSealed throws InvalidOperationException when the dictionary is read-only (sealed, typically because its owning ParserContext has been frozen after parsing). Sealed dictionaries may no longer be mutated: Clear, PushScope, PopScope, AddNamespace, and RemoveNamespace all funnel through this check.","triggerScenarios":"Calling Clear, PushScope, PopScope, AddNamespace, RemoveNamespace, Add, or the indexer setter on an XmlnsDictionary whose IsReadOnly is true (e.g. ParserContext.XmlnsDictionary of a context that finished parsing or was sealed).","commonSituations":"Reusing a cached ParserContext across multiple XAML parse operations and trying to register new namespaces on it; mutating a dictionary captured from a completed parse session.","solutions":["Create a new ParserContext / XmlnsDictionary for each parse instead of mutating a sealed one","Clone the sealed dictionary via the copy constructor new XmlnsDictionary(sealedDict) — the copy is mutable — then modify the copy","Check IsReadOnly before mutating and take the copy path when true"],"exampleFix":"// before\ncachedContext.XmlnsDictionary.AddNamespace(\"x\", \"http://example.com/x\"); // throws if sealed\n\n// after\nvar dict = cachedContext.XmlnsDictionary.IsReadOnly\n    ? new XmlnsDictionary(cachedContext.XmlnsDictionary)\n    : cachedContext.XmlnsDictionary;\ndict.AddNamespace(\"x\", \"http://example.com/x\");","handlingStrategy":"validation","validationCode":"if (dict.IsReadOnly)\n    dict = new XmlnsDictionary(dict); // mutable copy\nelse\n    dict.PushScope();","typeGuard":"bool IsMutable(XmlnsDictionary d) => !d.IsReadOnly;","tryCatchPattern":"try { dict.AddNamespace(\"x\", nsUri); }\ncatch (InvalidOperationException) { dict = new XmlnsDictionary(dict); dict.AddNamespace(\"x\", nsUri); }","preventionTips":["Check IsReadOnly before any mutation call","Never reuse a ParserContext across parse sessions for mutation; create a fresh one","Clone sealed dictionaries with the copy constructor instead of mutating them"],"tags":["wpf","xaml","read-only","invalid-state"],"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"}