dotnet/wpf · error · ArgumentException
ArgumentException(SR.NamespaceDeclarationCannotBeXml…
Error message
ArgumentException(SR.NamespaceDeclarationCannotBeXml, nameof(namespaceDeclaration))
What it means
The 'xml' prefix is reserved by the XML specification and is implicitly bound to http://www.w3.org/XML/1998/namespace. XamlXmlWriter.WriteNamespace rejects any attempt to explicitly declare a NamespaceDeclaration with Prefix == "xml", throwing ArgumentException.
Solutions
- Remove the explicit xml namespace declaration; it is implicit in all XML documents
- Filter out NamespaceDeclarations with Prefix "xml" before calling WriteNamespace
- Rename the prefix if a different namespace was intended
Example fix
// before foreach (var ns in namespaces) writer.WriteNamespace(ns); // includes prefix "xml" // after foreach (var ns in namespaces) if (ns.Prefix != "xml") writer.WriteNamespace(ns);
Defensive patterns
Strategy: validation
Validate before calling
if (nsDecl.Prefix == "xml") return; // xml prefix is implicit; skip declaration
Type guard
static bool IsReservedPrefix(NamespaceDeclaration d) => d.Prefix == "xml";
Try / catch
try { writer.WriteNamespace(nsDecl); }
catch (ArgumentException ex) when (ex.ParamName == "namespaceDeclaration") { /* log and skip reserved prefix */ } Prevention
- Never emit xmlns:xml; it is predefined in XML
- Filter reserved prefixes when forwarding namespaces from source documents
- Whitelist prefixes you actually intend to declare
When it happens
Trigger: Calling WriteNamespace with a NamespaceDeclaration whose Prefix equals "xml" (case-sensitive) regardless of the namespace URI supplied.
Common situations: Migrating hand-written XAML generation code that copied declarations verbatim from documents containing xmlns:xml; generic code that forwards every namespace seen in a source stream without filtering reserved prefixes.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- ArgumentException(SR.NamespaceDeclarationNamespaceCannotBeNu…
- ArgumentException(SR.NamespaceDeclarationPrefixCannotBeNull…
- nameof(declaringType)
- SR.AbsoluteUriNotAllowed
- SR.CollectionCannotContainNulls
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/b1fc04bee3e1e7aa.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs:240
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
{
return schemaContext;
}
}
View on GitHub (pinned to 81131a70a4)