dotnet/wpf · error · ArgumentNullException

throw new ArgumentNullException( nameof(prefix));

Error message

throw new ArgumentNullException( nameof(prefix));

What it means

This is a null-argument guard: XmlnsDictionary.LookupNamespace throws ArgumentNullException when the prefix parameter is null. Only non-null string prefixes (empty string allowed for the default namespace) may be looked up.

Solutions

  1. Guard with a null check before calling LookupNamespace and return early for null prefixes
  2. Treat an empty string as 'no prefix' (default namespace) rather than passing null

Example fix

// before
var ns = dictionary.LookupNamespace(prefix);

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

Strategy: type-guard

Validate before calling

var ns = prefix == null ? null : dictionary.LookupNamespace(prefix);

Type guard

bool HasPrefix(string prefix) => !string.IsNullOrEmpty(prefix);

Try / catch

try { ns = dictionary.LookupNamespace(prefix); }
catch (ArgumentNullException) { ns = null; }

Prevention

When it happens

Trigger: Calling xmlnsDictionary.LookupNamespace(null), typically forwarding an uninitialized prefix variable or a null result from an earlier lookup.

Common situations: Custom IXmlNamespaceResolver implementations delegating to XmlnsDictionary with a null prefix read from parsed markup; prefix fields not initialized before lookup.

Related errors


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

Appendix: source

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

        /// dynamic property). Any attempt to modify the dictionary after this function is called will result in
        /// a InvalidOperationException being thrown.
        /// </summary>
        public void Seal()
        {
            _sealed = true;
        }
#endif

        /// <summary>
        /// Looks up the namespace corresponding to an XML namespace prefix
        /// </summary>
        /// <param name="prefix">The XML namespace prefix to look up</param>
        /// <returns>The namespace corresponding to the given prefix if it exists, null otherwise</returns>
        public string LookupNamespace(string prefix)
        {
            if (prefix == null)
            {
                throw new ArgumentNullException( nameof(prefix)); 
            }
            
            if (_lastDecl >0)
            {
                for (int thisDecl = _lastDecl-1; thisDecl >= 0; thisDecl--)
                {
                    if ((_nsDeclarations[thisDecl].Prefix == prefix) && 
                        !string.IsNullOrEmpty(_nsDeclarations[thisDecl].Uri))
                    {
                        return _nsDeclarations[thisDecl].Uri;
                    }
                }
            }
            return null;
        }

#if !PBTCOMPILER
        /// <summary>

View on GitHub (pinned to 81131a70a4)