dotnet/wpf · error · ArgumentException

SR.NamespaceDeclarationNamespaceCannotBeNull

Error message

SR.NamespaceDeclarationNamespaceCannotBeNull

What it means

ArgumentException thrown by XamlObjectWriter.WriteNamespace when the supplied NamespaceDeclaration has a null Namespace (the namespace URI). Along with the prefix, the URI is mandatory for any namespace declaration the writer accepts.

Solutions

  1. Supply a valid namespace URI string when constructing the NamespaceDeclaration.
  2. Skip writing the declaration entirely if its URI is null rather than forwarding it.
  3. Guard before the call: if (ns.Namespace != null) writer.WriteNamespace(ns);

Example fix

// before
writer.WriteNamespace(new NamespaceDeclaration("x", null)); // throws
// after
if (ns.Namespace != null)
    writer.WriteNamespace(new NamespaceDeclaration(ns.Prefix ?? string.Empty, ns.Namespace));
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(ns.Namespace))
    throw new InvalidOperationException("NamespaceDeclaration requires a namespace URI."); // or skip the declaration

Type guard

static bool HasNamespace(NamespaceDeclaration ns) => ns?.Namespace != null;

Try / catch

try { writer.WriteNamespace(ns); }
catch (ArgumentException) { LogSkippedDeclaration(ns); } // drop invalid declaration

Prevention

When it happens

Trigger: Calling WriteNamespace(new NamespaceDeclaration(somePrefix, null)) — the namespace URI argument is null.

Common situations: Custom readers that lose the URI when collapsing duplicate xmlns declarations; constructing namespace declarations from parsed attributes where the value attribute was missing; generated node streams in tooling.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/XamlObjectWriter.cs:903

                        Logic_DoAssignmentToParentCollection(_context);
                        _context.PopScope();
                    }
                }
            }
        }

        public override void WriteNamespace(NamespaceDeclaration namespaceDeclaration)
        {
            ThrowIfDisposed();
            ArgumentNullException.ThrowIfNull(namespaceDeclaration);
            if (namespaceDeclaration.Prefix is null)
            {
                throw new ArgumentException(SR.NamespaceDeclarationPrefixCannotBeNull);
            }

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

            // Deferring Checking
            //
            _deferringWriter.WriteNamespace(namespaceDeclaration);
            if (_deferringWriter.Handled)
            {
                return;
            }

            // Error Checking
            //
            if (_nextNodeMustBeEndMember)
            {
                string err = SR.ValueMustBeFollowedByEndMember;
                throw _context.WithLineInfo(new XamlObjectWriterException(err));
            }

View on GitHub (pinned to 81131a70a4)