dotnet/wpf · error · ArgumentNullException

throw new ArgumentNullException(nameof(prefix));

Error message

throw new ArgumentNullException(nameof(prefix));

What it means

XmlnsDictionary.AddNamespace throws ArgumentNullException when the prefix argument is null. The dictionary maps XML namespace prefixes to namespace URIs for WPF XAML markup processing, and a null prefix has no meaning, so the library rejects it immediately after checking the dictionary is not sealed.

Solutions

  1. Ensure the prefix argument is a non-null string before calling Add/AddNamespace.
  2. If the prefix may be absent, skip the Add call or substitute a sensible default (e.g. empty string for the default namespace).
  3. Validate inputs when loading prefix mappings from external config before passing them in.

Example fix

// before
xmlnsDictionary.Add(prefix, namespaceUri);
// after
if (prefix == null) throw new InvalidOperationException("Prefix mapping missing from config");
xmlnsDictionary.Add(prefix, namespaceUri);
Defensive patterns

Strategy: validation

Validate before calling

if (prefix == null) throw new ArgumentException("prefix must be non-null", nameof(prefix));
xmlnsDictionary.Add(prefix, namespaceUri);

Type guard

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

Try / catch

try { xmlnsDictionary.Add(prefix, ns); }
catch (ArgumentNullException ex) { /* log missing prefix; skip mapping */ }

Prevention

When it happens

Trigger: Calling XmlnsDictionary.Add(prefix, xmlNamespace) (which forwards to AddNamespace) with a null prefix string, e.g. dictionary.Add(null, "http://...") or passing an uninitialized variable as the prefix.

Common situations: Building custom XAML namespaces programmatically, XAML schema context setup code, or deserializing prefix mappings from config where a prefix entry is missing.

Related errors


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

Appendix: source

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

                throw new InvalidOperationException(SR.ParserDictionarySealed);
            }
        }

        /// <summary>
        /// Helper to Add Namespace - Checks for prefix from rear.
        ///  If exists : override it locally, if not Adds an entry.
        /// </summary>
        /// <param name="prefix">prefix to add</param>
        /// <param name="xmlNamespace">namespace uri string to add</param>
        private void AddNamespace(string prefix, string xmlNamespace)
        {
            CheckSealed();
            
            if (xmlNamespace == null)
                throw new ArgumentNullException(nameof(xmlNamespace));

            if (prefix == null)
                throw new ArgumentNullException(nameof(prefix));

            int lastScopeCount = _nsDeclarations[_lastDecl].ScopeCount;

            if (_lastDecl > 0)
            {
                // Check the local scope for the given prefix
                for (int thisDecl = _lastDecl-1; 
                     thisDecl >= 0 && _nsDeclarations[thisDecl].ScopeCount == lastScopeCount; 
                     thisDecl--)
                {
                    if (String.Equals(_nsDeclarations[thisDecl].Prefix, prefix))
                    {
                        // Redefine an existing namespace
                        _nsDeclarations[thisDecl].Uri = xmlNamespace;
                        return; 
                    }
                }

View on GitHub (pinned to 81131a70a4)