{"record":{"id":"a0d9ecf49349edb9","repo":"dotnet/wpf","slug":"sr-parserkeysarestrings-dictionary-keys-and-values-must-be","errorCode":null,"errorMessage":"SR.ParserKeysAreStrings (dictionary keys and values must be strings)","messagePattern":"SR\\.ParserKeysAreStrings \\(dictionary keys and values must be strings\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlnsDictionary.cs","lineNumber":142,"sourceCode":"            }\n            else // create an empty array of Declarations. Default Size is 8. \n            {\n                Initialize();\n            }\n        }\n#endif\n\n        /// <summary>\n        /// Add a namespace to the dictionary (accepts objects for the purpose of implementing IDictionary)\n        /// </summary>\n        /// <param name=\"prefix\">The XML prefix of this namespace (should be a string)</param>\n        /// <param name=\"xmlNamespace\">The namespace the prefix maps to (should be a string)</param>\n        public void Add(object prefix, object xmlNamespace)\n        {\n            // Check for the parameters to be strings as this is IDictionary implementation with object type params.\n            if (!(prefix is string) || !(xmlNamespace is string))\n            {\n                throw new ArgumentException(SR.ParserKeysAreStrings);\n            }\n            // Add calls gets delegated to AddNamespace which adds a NamespaceDeclaration to the list of Declarations\n            AddNamespace((string)prefix, (string)xmlNamespace);\n         }\n\n#if !PBTCOMPILER\n        /// <summary>\n        /// Add a namespace to the dictionary\n        /// </summary>\n        /// <param name=\"prefix\">The XML prefix of this namespace</param>\n        /// <param name=\"xmlNamespace\">The namespace the prefix maps to</param>\n        public void Add(string prefix,string xmlNamespace)\n        {\n            // Add calls gets delegated to AddNamespace which adds a NamespaceDeclaration to the list of Declarations\n            AddNamespace(prefix, xmlNamespace);\n        }\n#endif\n","sourceCodeStart":124,"sourceCodeEnd":160,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlnsDictionary.cs#L124-L160","documentation":"XmlnsDictionary.Add(object, object) implements IDictionary with object parameters, but keys and values are constrained to strings because they represent XML namespace prefixes and URIs. Passing anything other than a string for either parameter throws ArgumentException with SR.ParserKeysAreStrings.","triggerScenarios":"Calling xmlnsDictionary.Add(prefix, ns) where prefix or xmlNamespace is not a string (e.g. an int, Uri, or XmlQualifiedName).","commonSituations":"Storing a System.Uri namespace instead of its string form; generic dictionary helper code boxing non-string keys; interop code treating the dictionary as an arbitrary IDictionary.","solutions":["Pass both prefix and xmlNamespace as strings (call .ToString() on Uri or other objects first)","Use the typed AddNamespace-like paths or Prefix/Namespace properties when available","Validate types before inserting into the dictionary"],"exampleFix":"// before\ndictionary.Add(\"x\", new Uri(\"http://schemas.example.com/main\"));\n\n// after\ndictionary.Add(\"x\", \"http://schemas.example.com/main\");","handlingStrategy":"validation","validationCode":"if (prefix is string p && xmlNamespace is string ns) dictionary.Add(p, ns);","typeGuard":"bool IsValidEntry(object k, object v) => k is string && v is string;","tryCatchPattern":"try { dictionary.Add(prefix, ns); }\ncatch (ArgumentException) { /* log: keys/values must be strings */ }","preventionTips":["Treat XmlnsDictionary as string-keyed/string-valued despite its IDictionary object signature","Normalize Uri objects with .ToString() before insertion","Add unit tests for namespace registration helpers with non-string inputs"],"tags":["wpf","xaml","type-mismatch","idictionary"],"backgroundTag":"type-mismatch","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"}