dotnet/wpf · error · ArgumentNullException

throw new ArgumentNullException( "uri" );

Error message

throw new ArgumentNullException( "uri" );

What it means

XmlAttributeProperties.LookupPrefix throws ArgumentNullException when the uri argument is null. The URI is compared against the element's default namespace and xmlns dictionaries; null is not a valid namespace URI here.

Solutions

  1. Only call LookupPrefix when the URI is a valid non-null string (use string.Empty for the default namespace)
  2. Guard: if (string.IsNullOrEmpty(uri)) return null; before the call
  3. Fix the upstream URI extraction to yield string.Empty rather than null

Example fix

// before
var prefix = XmlAttributeProperties.LookupPrefix(element, resolvedNamespaceUri); // may be null
// after
var prefix = resolvedNamespaceUri != null
    ? XmlAttributeProperties.LookupPrefix(element, resolvedNamespaceUri)
    : null;
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(uri)) return null;
var prefix = XmlAttributeProperties.LookupPrefix(element, uri);

Type guard

static bool IsValidNamespaceUri(string u) => !string.IsNullOrEmpty(u);

Try / catch

try { prefix = XmlAttributeProperties.LookupPrefix(element, uri); }
catch (ArgumentNullException ex) { prefix = null; Log.Warn("Namespace URI was null"); }

Prevention

When it happens

Trigger: Calling LookupPrefix(elem, null) — usually because the namespace URI came from XML attribute parsing where the xmlns attribute was absent, or a lookup that returned null (e.g. from GetNamespace on a missing prefix).

Common situations: Code that captures a namespace URI from an XmlReader/XmlNode and stores null for absent namespaces, then forwards it to LookupPrefix; schema-driven generation where optional namespaces were omitted.

Related errors


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

Appendix: source

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

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

            // Following the letter of the specification, the default namespace should take 
            // precedence in the case of redundancy, so we check that first.
            if (DefaultNamespace(elem) == uri) 
                return string.Empty;

            while (null != elem)
            {
                XmlnsDictionary d = GetXmlnsDictionary(elem);
                if (null != d)
                {
                    // Search through all key/value pairs in the dictionary
                    foreach (DictionaryEntry e in d)
                    {
                        if ((string)e.Value == uri && LookupNamespace(oldElem, (string)e.Key) == uri)
                        {
                            return (string)e.Key;

View on GitHub (pinned to 81131a70a4)