{"record":{"id":"b1fc04bee3e1e7aa","repo":"dotnet/wpf","slug":"argumentexception-sr-namespacedeclarationcannotbexml-nameof","errorCode":null,"errorMessage":"ArgumentException(SR.NamespaceDeclarationCannotBeXml, nameof(namespaceDeclaration))","messagePattern":"ArgumentException\\(SR\\.NamespaceDeclarationCannotBeXml, nameof\\(namespaceDeclaration\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs","lineNumber":240,"sourceCode":"        public override void WriteNamespace(NamespaceDeclaration namespaceDeclaration)\n        {\n            CheckIsDisposed();\n\n            ArgumentNullException.ThrowIfNull(namespaceDeclaration);\n\n            if (namespaceDeclaration.Prefix is null)\n            {\n                throw new ArgumentException(SR.NamespaceDeclarationPrefixCannotBeNull, nameof(namespaceDeclaration));\n            }\n\n            if (namespaceDeclaration.Namespace is null)\n            {\n                throw new ArgumentException(SR.NamespaceDeclarationNamespaceCannotBeNull, nameof(namespaceDeclaration));\n            }\n\n            if (namespaceDeclaration.Prefix == \"xml\")\n            {\n                throw new ArgumentException(SR.NamespaceDeclarationCannotBeXml, nameof(namespaceDeclaration));\n            }\n\n            currentState.WriteNamespace(this, namespaceDeclaration);\n        }\n\n        public XamlXmlWriterSettings Settings\n        {\n            get { return settings.Copy() as XamlXmlWriterSettings; }\n        }\n\n        public override XamlSchemaContext SchemaContext\n        {\n            get\n            {\n                return schemaContext;\n            }\n        }\n","sourceCodeStart":222,"sourceCodeEnd":258,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs#L222-L258","documentation":"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.","triggerScenarios":"Calling WriteNamespace with a NamespaceDeclaration whose Prefix equals \"xml\" (case-sensitive) regardless of the namespace URI supplied.","commonSituations":"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.","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"],"exampleFix":"// before\nforeach (var ns in namespaces) writer.WriteNamespace(ns); // includes prefix \"xml\"\n// after\nforeach (var ns in namespaces) if (ns.Prefix != \"xml\") writer.WriteNamespace(ns);","handlingStrategy":"validation","validationCode":"if (nsDecl.Prefix == \"xml\") return; // xml prefix is implicit; skip declaration","typeGuard":"static bool IsReservedPrefix(NamespaceDeclaration d) => d.Prefix == \"xml\";","tryCatchPattern":"try { writer.WriteNamespace(nsDecl); }\ncatch (ArgumentException ex) when (ex.ParamName == \"namespaceDeclaration\") { /* log and skip reserved prefix */ }","preventionTips":["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"],"tags":["xaml","argument-validation","xml-namespace"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}