dotnet/wpf · error · ArgumentException

ArgumentException(SR.NamespaceDeclarationNamespaceCannotBeNu…

Error message

ArgumentException(SR.NamespaceDeclarationNamespaceCannotBeNull, nameof(namespaceDeclaration))

What it means

XamlXmlWriter.WriteNamespace requires a non-null Namespace URI. A NamespaceDeclaration whose Namespace property is null has nothing to map a prefix to, so the writer throws ArgumentException naming the parameter.

Solutions

  1. Set the Namespace URI on the NamespaceDeclaration before writing it
  2. Check the XamlType/xamlNamespace resolution that produced the declaration
  3. Skip or replace declarations with null Namespace instead of writing them

Example fix

// before
writer.WriteNamespace(new NamespaceDeclaration(null, "x"));
// after
writer.WriteNamespace(new NamespaceDeclaration("http://schemas.microsoft.com/winfx/2006/xaml", "x"));
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(nsDecl.Namespace)) throw new ArgumentException("Namespace URI must be non-null before writing", nameof(nsDecl));

Type guard

static bool HasNamespace(NamespaceDeclaration? d) => d?.Namespace is not null;

Try / catch

try { writer.WriteNamespace(nsDecl); }
catch (ArgumentException ex) when (ex.ParamName == "namespaceDeclaration") { /* substitute a valid URI or skip */ }

Prevention

When it happens

Trigger: Calling WriteNamespace with a NamespaceDeclaration whose Namespace property is null, e.g. new NamespaceDeclaration(null, "x") or one built from a XamlType whose Namespace was never resolved.

Common situations: Constructing declarations from reflection data where the CLR namespace failed to map to an XML namespace; copying declarations from a parsed stream where the URI was omitted; typos passing arguments in the wrong order to the NamespaceDeclaration constructor.

Related errors


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

Appendix: source

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

                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; }
        }

        public override XamlSchemaContext SchemaContext
        {
            get

View on GitHub (pinned to 81131a70a4)