{"record":{"id":"d860b65d19abf683","repo":"dotnet/wpf","slug":"argumentexception-sr-namespacedeclarationprefixcannotbenull","errorCode":null,"errorMessage":"ArgumentException(SR.NamespaceDeclarationPrefixCannotBeNull, nameof(namespaceDeclaration))","messagePattern":"ArgumentException\\(SR\\.NamespaceDeclarationPrefixCannotBeNull, nameof\\(namespaceDeclaration\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs","lineNumber":230,"sourceCode":"            {\n                if (value is not string s)\n                {\n                    throw new ArgumentException(SR.XamlXmlWriterCannotWriteNonstringValue, nameof(value));\n                }\n\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; }","sourceCodeStart":212,"sourceCodeEnd":248,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs#L212-L248","documentation":"XamlXmlWriter.WriteNamespace validates each NamespaceDeclaration before emitting it. A NamespaceDeclaration with a null Prefix cannot be serialized to XML because every xmlns declaration requires a prefix (or the empty default-prefix case is represented by \"\", not null). The writer throws ArgumentException naming the offending parameter.","triggerScenarios":"Calling XamlXmlWriter.WriteNamespace with a NamespaceDeclaration constructed or deserialized such that its Prefix property is null, e.g. new NamespaceDeclaration(\"http://schemas.microsoft.com/winfx/2006/xaml\", null).","commonSituations":"Building namespace declarations programmatically from data where the prefix field was never initialized; deserializing namespace metadata from config/JSON that omitted the prefix; passing a default-initialized struct-like object.","solutions":["Ensure the NamespaceDeclaration.Prefix is set before calling WriteNamespace","Use an empty string \"\" instead of null for a default (unnamed) xmlns prefix","Validate all NamespaceDeclaration objects before passing them to the writer"],"exampleFix":"// before\nwriter.WriteNamespace(new NamespaceDeclaration(nsUri, null));\n// after\nwriter.WriteNamespace(new NamespaceDeclaration(nsUri, \"\")); // default ns uses empty prefix, not null","handlingStrategy":"validation","validationCode":"if (nsDecl is null) throw new ArgumentNullException(nameof(nsDecl));\nif (nsDecl.Prefix is null) throw new ArgumentException(\"Prefix must not be null (use \\\"\\\" for default ns)\", nameof(nsDecl));","typeGuard":"static bool IsValidNamespaceDeclaration(NamespaceDeclaration? d) => d is not null && d.Prefix is not null && d.Namespace is not null;","tryCatchPattern":"try { writer.WriteNamespace(nsDecl); }\ncatch (ArgumentException ex) when (ex.ParamName == \"namespaceDeclaration\") { /* fix or skip declaration */ }","preventionTips":["Treat null Prefix as invalid; default namespace uses empty string \"\"","Validate NamespaceDeclaration objects at construction time","Never build declarations from uninitialized fields"],"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"}