dotnet/wpf · error · ArgumentNullException

ArgumentNullException(nameof(xmlNamespace))

Error message

ArgumentNullException(nameof(xmlNamespace))

What it means

XamlTypeMapper.GetType(xmlNamespace, localName) resolves a XAML namespace plus local type name to a CLR Type. It validates both parameters and throws ArgumentNullException when xmlNamespace is null. The namespace is the key used to look up the cached TypeAndSerializer entry, so a null namespace cannot be resolved.

Solutions

  1. Ensure the xmlNamespace string is non-null before calling GetType; use String.Empty for the default namespace if appropriate.
  2. Resolve the namespace from the ParserContext's XmlnsDictionary and fall back to a known constant (e.g. XamlReaderHelper.DefaultNamespace) when absent.
  3. Wrap the call and convert the ArgumentNullException into a domain-specific error identifying the malformed XAML node.

Example fix

// before
Type t = mapper.GetType(node.Namespace, node.LocalName); // node.Namespace may be null
// after
string ns = node.Namespace ?? XamlReaderHelper.DefaultNamespace;
Type t = mapper.GetType(ns, node.LocalName);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(xmlNamespace))
    xmlNamespace = XamlReaderHelper.DefaultNamespace; // or skip the lookup

Type guard

static bool CanResolve(string xmlNamespace, string localName) =>
    xmlNamespace != null && localName != null;

Try / catch

try
{
    type = mapper.GetType(xmlNamespace, localName);
}
catch (ArgumentNullException ex) when (ex.ParamName == "xmlNamespace")
{
    throw new XamlParseException($"Element '{localName}' has no xmlns in scope", ex);
}

Prevention

When it happens

Trigger: Calling mapper.GetType(null, "Button") — passing a null XML namespace string directly to XamlTypeMapper.GetType, typically from custom GetTypeFromBaseString or GetTypeArgsType logic.

Common situations: Custom markup extension or type-resolution code derives the namespace from a parsed XAML node that had no xmlns in scope, or from an XmlnsDictionary lookup that returned null.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlTypeMapper.cs:105

        /// </summary>
        /// <remarks>
        /// Example:<para/>
        ///     If the xml contained the tags <base:Button xmlns:base="AvalonBase"/>
        ///     you would call XamlTypeMapper.GetType("AvalonBase","Button");
        ///     <para/>
        ///     Note the XmlNamespace "AvalonBase" is the actual namespace value, not
        ///     the base: prefix.
        /// </remarks>
        /// <param name="xmlNamespace">NamespaceURI of tag</param>
        /// <param name="localName">localName of the Tag</param>
        /// <returns>Type for the object. If no type was found NULL is returned</returns>
        public Type GetType(
            string xmlNamespace,
            string localName)
        {
            if(null == xmlNamespace)
            {
                throw new ArgumentNullException( nameof(xmlNamespace));
            }
            if(null == localName)
            {
                throw new ArgumentNullException( nameof(localName));
            }

            TypeAndSerializer typeAndSerializer =
                GetTypeOnly(xmlNamespace,localName);

            return typeAndSerializer?.ObjectType;
        }

#if !PBTCOMPILER
        /// <summary>
        ///  Programmatic counterpart to the <?Mapping ... ?> XAML PI.  For example, <para/>
        ///    <?Mapping XmlNamespace="swc" ClrNamespace="System.Windows.ComponentModel" Assembly="PresentationFramework" ?>
        /// </summary>
        /// <param name="xmlNamespace">

View on GitHub (pinned to 81131a70a4)