dotnet/wpf · error · ArgumentException

SR.ParserKeysAreStrings (dictionary keys and values must be…

Error message

SR.ParserKeysAreStrings (dictionary keys and values must be strings)

What it means

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.

Solutions

  1. Pass both prefix and xmlNamespace as strings (call .ToString() on Uri or other objects first)
  2. Use the typed AddNamespace-like paths or Prefix/Namespace properties when available
  3. Validate types before inserting into the dictionary

Example fix

// before
dictionary.Add("x", new Uri("http://schemas.example.com/main"));

// after
dictionary.Add("x", "http://schemas.example.com/main");
Defensive patterns

Strategy: validation

Validate before calling

if (prefix is string p && xmlNamespace is string ns) dictionary.Add(p, ns);

Type guard

bool IsValidEntry(object k, object v) => k is string && v is string;

Try / catch

try { dictionary.Add(prefix, ns); }
catch (ArgumentException) { /* log: keys/values must be strings */ }

Prevention

When it happens

Trigger: Calling xmlnsDictionary.Add(prefix, ns) where prefix or xmlNamespace is not a string (e.g. an int, Uri, or XmlQualifiedName).

Common situations: 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.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/a0d9ecf49349edb9. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlnsDictionary.cs:142

            }
            else // create an empty array of Declarations. Default Size is 8. 
            {
                Initialize();
            }
        }
#endif

        /// <summary>
        /// Add a namespace to the dictionary (accepts objects for the purpose of implementing IDictionary)
        /// </summary>
        /// <param name="prefix">The XML prefix of this namespace (should be a string)</param>
        /// <param name="xmlNamespace">The namespace the prefix maps to (should be a string)</param>
        public void Add(object prefix, object xmlNamespace)
        {
            // Check for the parameters to be strings as this is IDictionary implementation with object type params.
            if (!(prefix is string) || !(xmlNamespace is string))
            {
                throw new ArgumentException(SR.ParserKeysAreStrings);
            }
            // Add calls gets delegated to AddNamespace which adds a NamespaceDeclaration to the list of Declarations
            AddNamespace((string)prefix, (string)xmlNamespace);
         }

#if !PBTCOMPILER
        /// <summary>
        /// Add a namespace to the dictionary
        /// </summary>
        /// <param name="prefix">The XML prefix of this namespace</param>
        /// <param name="xmlNamespace">The namespace the prefix maps to</param>
        public void Add(string prefix,string xmlNamespace)
        {
            // Add calls gets delegated to AddNamespace which adds a NamespaceDeclaration to the list of Declarations
            AddNamespace(prefix, xmlNamespace);
        }
#endif

View on GitHub (pinned to 81131a70a4)