dotnet/wpf · error · ArgumentNullException

throw new ArgumentNullException( "prefix" );

Error message

throw new ArgumentNullException( "prefix" );

What it means

XmlAttributeProperties.LookupNamespace throws ArgumentNullException when the prefix argument is null. The prefix is used both as a dictionary key in the element's XmlnsDictionary and to walk up the parent chain, so a null prefix is invalid input.

Solutions

  1. Pass string.Empty instead of null when the default (no-prefix) namespace is intended
  2. Guard: if (prefix == null) prefix = string.Empty; before the call
  3. Fix prefix extraction so unprefixed names consistently yield empty string

Example fix

// before
var ns = XmlAttributeProperties.LookupNamespace(element, attributeNamePrefix); // null for default ns
// after
var ns = XmlAttributeProperties.LookupNamespace(element, attributeNamePrefix ?? string.Empty);
Defensive patterns

Strategy: validation

Validate before calling

if (prefix == null) prefix = string.Empty;
var ns = XmlAttributeProperties.LookupNamespace(element, prefix);

Type guard

static bool IsValidPrefix(string p) => p != null;

Try / catch

try { ns = XmlAttributeProperties.LookupNamespace(element, prefix); }
catch (ArgumentNullException ex) { ns = null; Log.Warn("Prefix was null; use string.Empty for the default namespace"); }

Prevention

When it happens

Trigger: Calling LookupNamespace(elem, null) — typically when the prefix was extracted from a parsed attribute name and the extraction produced null (e.g. an attribute without a prefix handled inconsistently).

Common situations: Custom XAML parsers or markup processing code splitting 'Prefix:LocalName' incorrectly; code that conflates the default namespace (empty string) with null.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XmlAttributeProperties.cs:82

#if Lookups
        /// <summary>
        /// Looks up the namespace corresponding to an XML namespace prefix
        /// </summary>
        /// <param name="elem">The DependencyObject containing the namespace mappings to be 
        ///  used in the lookup</param>
        /// <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 static string LookupNamespace(DependencyObject elem, string prefix)
        {
            if (elem == null)
            {
                throw new ArgumentNullException( "elem" ); 
            }
            if (prefix == null)
            {
                throw new ArgumentNullException( "prefix" ); 
            }

            // Walk up the parent chain until one of the parents has the passed prefix in 
            // its xmlns dictionary, or until a null parent is reached.
            XmlnsDictionary d = GetXmlnsDictionary(elem);
            while ((null == d || null == d[prefix]) && null != elem)
            {
                elem = LogicalTreeHelper.GetParent(elem);
                if (elem != null)
                {
                    d = GetXmlnsDictionary(elem);
                }
            }
            
            if (null != d)
            {
                return d[prefix];
            }

View on GitHub (pinned to 81131a70a4)