dotnet/wpf · error · ArgumentNullException

throw new ArgumentNullException(nameof(xmlNamespace));

Error message

throw new ArgumentNullException(nameof(xmlNamespace));

What it means

XmlnsDictionary.AddNamespace(string, string) throws ArgumentNullException when the xmlNamespace or prefix argument is null. After CheckSealed, both parameters must be non-null strings because they define an XML namespace declaration (prefix -> URI).

Solutions

  1. Ensure the namespace URI and prefix are non-null before calling
  2. Substitute string.Empty or a default namespace when a null URI is acceptable in your scenario
  3. Validate configuration values feeding namespace registrations

Example fix

// before
dictionary.AddNamespace("x", configNs); // configNs may be null

// after
if (configNs != null)
{
    dictionary.AddNamespace("x", configNs);
}
Defensive patterns

Strategy: validation

Validate before calling

if (prefix != null && xmlNamespace != null) dictionary.AddNamespace(prefix, xmlNamespace);

Type guard

bool CanAddNamespace(string prefix, string ns) => prefix != null && ns != null;

Try / catch

try { dictionary.AddNamespace(prefix, nsUri); }
catch (ArgumentNullException) { /* skip or log missing prefix/namespace */ }

Prevention

When it happens

Trigger: Calling AddNamespace("x", null) or AddNamespace(null, ns) directly, or indirectly via Add(object, object) with a null value cast to string, or via the indexer setter with a null value.

Common situations: Registering namespaces from configuration where a URI entry was empty/null; passing null values through generic IDictionary helper code.

Related errors


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

Appendix: source

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

        {
            if (IsReadOnly)
            {
                throw new InvalidOperationException(SR.ParserDictionarySealed);
            }
        }

        /// <summary>
        /// Helper to Add Namespace - Checks for prefix from rear.
        ///  If exists : override it locally, if not Adds an entry.
        /// </summary>
        /// <param name="prefix">prefix to add</param>
        /// <param name="xmlNamespace">namespace uri string to add</param>
        private void AddNamespace(string prefix, string xmlNamespace)
        {
            CheckSealed();
            
            if (xmlNamespace == null)
                throw new ArgumentNullException(nameof(xmlNamespace));

            if (prefix == null)
                throw new ArgumentNullException(nameof(prefix));

            int lastScopeCount = _nsDeclarations[_lastDecl].ScopeCount;

            if (_lastDecl > 0)
            {
                // Check the local scope for the given prefix
                for (int thisDecl = _lastDecl-1; 
                     thisDecl >= 0 && _nsDeclarations[thisDecl].ScopeCount == lastScopeCount; 
                     thisDecl--)
                {
                    if (String.Equals(_nsDeclarations[thisDecl].Prefix, prefix))
                    {
                        // Redefine an existing namespace
                        _nsDeclarations[thisDecl].Uri = xmlNamespace;
                        return; 

View on GitHub (pinned to 81131a70a4)