dotnet/wpf · error · ArgumentNullException

throw new ArgumentNullException( nameof(xmlNamespace));

Error message

throw new ArgumentNullException( nameof(xmlNamespace));

What it means

XmlnsDictionary.LookupPrefix(string) throws ArgumentNullException when the xmlNamespace argument is null. The method performs a reverse lookup (namespace to prefix), which is meaningless for a null namespace.

Solutions

  1. Null-check the namespace string before calling LookupPrefix
  2. Default null namespaces to a safe value (e.g. string.Empty) when a lookup result placeholder is needed

Example fix

// before
var prefix = dictionary.LookupPrefix(xmlNamespace);

// after
var prefix = xmlNamespace != null ? dictionary.LookupPrefix(xmlNamespace) : null;
Defensive patterns

Strategy: type-guard

Validate before calling

var prefix = xmlNamespace == null ? null : dictionary.LookupPrefix(xmlNamespace);

Type guard

bool HasNamespace(string ns) => !string.IsNullOrEmpty(ns);

Try / catch

try { prefix = dictionary.LookupPrefix(xmlNamespace); }
catch (ArgumentNullException) { prefix = null; }

Prevention

When it happens

Trigger: Calling xmlnsDictionary.LookupPrefix(null), e.g. when the namespace string came from an unchecked API result or deserialized value.

Common situations: Reverse-mapping namespaces during XAML serialization when a namespace URI variable was never assigned; custom namespace resolver code forwarding null.

Related errors


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

Appendix: source

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

            }
            return null;
        }

#if !PBTCOMPILER
        /// <summary>
        /// Looks up the XML prefix corresponding to a namespaceuri
        /// </summary>
        /// <param name="xmlNamespace">The namespaceuri to look up</param>
        /// <returns>
        /// string.Empty if the given namespace corresponds to the default namespace; 
        /// otherwise, the XML prefix corresponding to the given namespace, or null 
        /// if none exists.
        /// </returns>
        public string LookupPrefix(string xmlNamespace)
        {
            if (xmlNamespace == null)
            {
                throw new ArgumentNullException( nameof(xmlNamespace)); 
            }

            if (_lastDecl > 0)
            {
                for (int thisDecl = _lastDecl-1; thisDecl >= 0; thisDecl--)
                {
                    if (_nsDeclarations[thisDecl].Uri == xmlNamespace)
                        return _nsDeclarations[thisDecl].Prefix;         
                }
            }
            return null;
       }

        /// <summary>
        /// DefaultNamespace for easy Access.
        /// </summary>
        public string DefaultNamespace()
        {

View on GitHub (pinned to 81131a70a4)