dotnet/wpf · error · ArgumentException

ArgumentException(SR.NamespaceDeclarationPrefixCannotBeNull…

Error message

ArgumentException(SR.NamespaceDeclarationPrefixCannotBeNull, nameof(namespaceDeclaration))

What it means

XamlXmlWriter.WriteNamespace validates each NamespaceDeclaration before emitting it. A NamespaceDeclaration with a null Prefix cannot be serialized to XML because every xmlns declaration requires a prefix (or the empty default-prefix case is represented by "", not null). The writer throws ArgumentException naming the offending parameter.

Solutions

  1. Ensure the NamespaceDeclaration.Prefix is set before calling WriteNamespace
  2. Use an empty string "" instead of null for a default (unnamed) xmlns prefix
  3. Validate all NamespaceDeclaration objects before passing them to the writer

Example fix

// before
writer.WriteNamespace(new NamespaceDeclaration(nsUri, null));
// after
writer.WriteNamespace(new NamespaceDeclaration(nsUri, "")); // default ns uses empty prefix, not null
Defensive patterns

Strategy: validation

Validate before calling

if (nsDecl is null) throw new ArgumentNullException(nameof(nsDecl));
if (nsDecl.Prefix is null) throw new ArgumentException("Prefix must not be null (use \"\" for default ns)", nameof(nsDecl));

Type guard

static bool IsValidNamespaceDeclaration(NamespaceDeclaration? d) => d is not null && d.Prefix is not null && d.Namespace is not null;

Try / catch

try { writer.WriteNamespace(nsDecl); }
catch (ArgumentException ex) when (ex.ParamName == "namespaceDeclaration") { /* fix or skip declaration */ }

Prevention

When it happens

Trigger: Calling XamlXmlWriter.WriteNamespace with a NamespaceDeclaration constructed or deserialized such that its Prefix property is null, e.g. new NamespaceDeclaration("http://schemas.microsoft.com/winfx/2006/xaml", null).

Common situations: Building namespace declarations programmatically from data where the prefix field was never initialized; deserializing namespace metadata from config/JSON that omitted the prefix; passing a default-initialized struct-like object.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs:230

            {
                if (value is not string s)
                {
                    throw new ArgumentException(SR.XamlXmlWriterCannotWriteNonstringValue, nameof(value));
                }

                currentState.WriteValue(this, s);
            }
        }

        public override void WriteNamespace(NamespaceDeclaration namespaceDeclaration)
        {
            CheckIsDisposed();

            ArgumentNullException.ThrowIfNull(namespaceDeclaration);

            if (namespaceDeclaration.Prefix is null)
            {
                throw new ArgumentException(SR.NamespaceDeclarationPrefixCannotBeNull, nameof(namespaceDeclaration));
            }

            if (namespaceDeclaration.Namespace is null)
            {
                throw new ArgumentException(SR.NamespaceDeclarationNamespaceCannotBeNull, nameof(namespaceDeclaration));
            }

            if (namespaceDeclaration.Prefix == "xml")
            {
                throw new ArgumentException(SR.NamespaceDeclarationCannotBeXml, nameof(namespaceDeclaration));
            }

            currentState.WriteNamespace(this, namespaceDeclaration);
        }

        public XamlXmlWriterSettings Settings
        {
            get { return settings.Copy() as XamlXmlWriterSettings; }

View on GitHub (pinned to 81131a70a4)