{"record":{"id":"63687117e729dd5b","repo":"dotnet/wpf","slug":"argumentexception-sr","errorCode":null,"errorMessage":"ArgumentException(SR.NamespaceDeclarationNamespaceCannotBeNull, nameof(namespaceDeclaration))","messagePattern":"ArgumentException\\(SR\\.NamespaceDeclarationNamespaceCannotBeNull, nameof\\(namespaceDeclaration\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs","lineNumber":235,"sourceCode":"\n                currentState.WriteValue(this, s);\n            }\n        }\n\n        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","sourceCodeStart":217,"sourceCodeEnd":253,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs#L217-L253","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Set the Namespace URI on the NamespaceDeclaration before writing it","Check the XamlType/xamlNamespace resolution that produced the declaration","Skip or replace declarations with null Namespace instead of writing them"],"exampleFix":"// before\nwriter.WriteNamespace(new NamespaceDeclaration(null, \"x\"));\n// after\nwriter.WriteNamespace(new NamespaceDeclaration(\"http://schemas.microsoft.com/winfx/2006/xaml\", \"x\"));","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(nsDecl.Namespace)) throw new ArgumentException(\"Namespace URI must be non-null before writing\", nameof(nsDecl));","typeGuard":"static bool HasNamespace(NamespaceDeclaration? d) => d?.Namespace is not null;","tryCatchPattern":"try { writer.WriteNamespace(nsDecl); }\ncatch (ArgumentException ex) when (ex.ParamName == \"namespaceDeclaration\") { /* substitute a valid URI or skip */ }","preventionTips":["Resolve the XAML namespace for a CLR namespace before building the declaration","Check XamlType.PreferredXamlNamespace instead of assuming a URI","Filter declarations with null Namespace out of write loops"],"tags":["xaml","argument-validation","null-value"],"backgroundTag":"null-argument","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"}