dotnet/wpf · error · ArgumentException
SR.NamespaceDeclarationPrefixCannotBeNull
Error message
SR.NamespaceDeclarationPrefixCannotBeNull
What it means
ArgumentException thrown by XamlObjectWriter.WriteNamespace when the supplied NamespaceDeclaration has a null Prefix. A namespace declaration must carry both a prefix and a namespace URI; System.Xaml validates both and rejects null prefixes before forwarding the declaration to the underlying writers.
Solutions
- Pass string.Empty as the prefix for the default namespace instead of null.
- Guard the prefix with a null check and substitute string.Empty before constructing the NamespaceDeclaration.
- Fix the upstream XML parser/reader mapping so xmlns (default) namespaces yield an empty prefix, not null.
Example fix
// before writer.WriteNamespace(new NamespaceDeclaration(null, "http://schemas.microsoft.com/winfx/2006/xaml")); // after string prefix = ns.Prefix ?? string.Empty; // default namespace uses empty prefix writer.WriteNamespace(new NamespaceDeclaration(prefix, "http://schemas.microsoft.com/winfx/2006/xaml"));
Defensive patterns
Strategy: validation
Validate before calling
if (ns.Prefix == null) ns = new NamespaceDeclaration(string.Empty, ns.Namespace); // normalize default prefix
Type guard
static bool HasPrefix(NamespaceDeclaration ns) => ns?.Prefix != null;
Try / catch
try { writer.WriteNamespace(ns); }
catch (ArgumentException) { ns = new NamespaceDeclaration(string.Empty, ns.Namespace); writer.WriteNamespace(ns); } Prevention
- Map the default XML namespace (xmlns) to string.Empty, never null.
- Normalize namespace declarations at the reader/transform boundary.
- Null-check Prefix before constructing NamespaceDeclaration.
- Test node-stream producers against default-namespace documents.
When it happens
Trigger: Calling WriteNamespace(new NamespaceDeclaration(null, someNamespace)) — the prefix argument is null.
Common situations: Custom XAML node-stream producers reading prefixes from parsed XML where the default (empty) namespace was represented as null instead of string.Empty; hand-built node streams in tests or transformers.
Related errors
- SR.NamespaceDeclarationNamespaceCannotBeNull
- SR.SavedContextSchemaContextNull
- ArgumentException(SR.Format(SR.CollectionCannotContainNulls…
- ArgumentException(SR.StringIsNullOrEmpty, nameof(fileName))
- ArgumentNullException(nameof(newNamespace))
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/b199456ae2cb1b04.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/XamlObjectWriter.cs:898
_context.PopScope();
}
else
{
_context.CurrentInstance = value;
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)View on GitHub (pinned to 81131a70a4)