{"record":{"id":"b199456ae2cb1b04","repo":"dotnet/wpf","slug":"sr-namespacedeclarationprefixcannotbenull","errorCode":null,"errorMessage":"SR.NamespaceDeclarationPrefixCannotBeNull","messagePattern":"SR\\.NamespaceDeclarationPrefixCannotBeNull","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/XamlObjectWriter.cs","lineNumber":898,"sourceCode":"                        _context.PopScope();\n                    }\n                    else\n                    {\n                        _context.CurrentInstance = value;\n                        Logic_DoAssignmentToParentCollection(_context);\n                        _context.PopScope();\n                    }\n                }\n            }\n        }\n\n        public override void WriteNamespace(NamespaceDeclaration namespaceDeclaration)\n        {\n            ThrowIfDisposed();\n            ArgumentNullException.ThrowIfNull(namespaceDeclaration);\n            if (namespaceDeclaration.Prefix is null)\n            {\n                throw new ArgumentException(SR.NamespaceDeclarationPrefixCannotBeNull);\n            }\n\n            if (namespaceDeclaration.Namespace is null)\n            {\n                throw new ArgumentException(SR.NamespaceDeclarationNamespaceCannotBeNull);\n            }\n\n            // Deferring Checking\n            //\n            _deferringWriter.WriteNamespace(namespaceDeclaration);\n            if (_deferringWriter.Handled)\n            {\n                return;\n            }\n\n            // Error Checking\n            //\n            if (_nextNodeMustBeEndMember)","sourceCodeStart":880,"sourceCodeEnd":916,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/XamlObjectWriter.cs#L880-L916","documentation":"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.","triggerScenarios":"Calling WriteNamespace(new NamespaceDeclaration(null, someNamespace)) — the prefix argument is null.","commonSituations":"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.","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."],"exampleFix":"// before\nwriter.WriteNamespace(new NamespaceDeclaration(null, \"http://schemas.microsoft.com/winfx/2006/xaml\"));\n// after\nstring prefix = ns.Prefix ?? string.Empty; // default namespace uses empty prefix\nwriter.WriteNamespace(new NamespaceDeclaration(prefix, \"http://schemas.microsoft.com/winfx/2006/xaml\"));","handlingStrategy":"validation","validationCode":"if (ns.Prefix == null) ns = new NamespaceDeclaration(string.Empty, ns.Namespace); // normalize default prefix","typeGuard":"static bool HasPrefix(NamespaceDeclaration ns) => ns?.Prefix != null;","tryCatchPattern":"try { writer.WriteNamespace(ns); }\ncatch (ArgumentException) { ns = new NamespaceDeclaration(string.Empty, ns.Namespace); writer.WriteNamespace(ns); }","preventionTips":["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."],"tags":["xaml","argumentexception","namespace","null-check"],"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"}