{"record":{"id":"f4de0e23193b47c0","repo":"dotnet/wpf","slug":"sr-bamlwriterbadxmlns-0-1","errorCode":null,"errorMessage":"SR.BamlWriterBadXmlns: {0} {1}","messagePattern":"SR\\.BamlWriterBadXmlns: (.+?) (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlWriter.cs","lineNumber":470,"sourceCode":"        /// <remarks>\n        /// This is used for setting the default namespace, or for mapping\n        /// a namespace prefix (or localName) to a namespace.  If prefix is \n        /// an empty string, then this sets the default namespace.\n        /// </remarks>\n        public void WriteXmlnsProperty(\n            string  localName,\n            string  xmlNamespace)\n        {\n            VerifyWriteState();\n            \n            BamlRecordType parentType = PeekRecordType();\n            if (parentType != BamlRecordType.ElementStart &&\n                parentType != BamlRecordType.PropertyComplexStart &&\n                parentType != BamlRecordType.PropertyArrayStart &&\n                parentType != BamlRecordType.PropertyIListStart &&\n                parentType != BamlRecordType.PropertyIDictionaryStart)\n            {\n                throw new InvalidOperationException(SR.Format(SR.BamlWriterBadXmlns,\n                                                           \"WriteXmlnsProperty\",\n                                                           parentType.ToString()));\n            }\n            \n            XamlXmlnsPropertyNode xmlnsNode = new XamlXmlnsPropertyNode(\n                                                    0,\n                                                    0,\n                                                    _depth,\n                                                    localName,\n                                                    xmlNamespace);\n        \n            _bamlRecordWriter.WriteNamespacePrefix(xmlnsNode);\n            _parserContext.XmlnsDictionary[localName] = xmlNamespace;\n        }\n\n        /// <summary>\n        /// Write an attribute in the Definition namespace.  \n        /// </summary>","sourceCodeStart":452,"sourceCodeEnd":488,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlWriter.cs#L452-L488","documentation":"WriteXmlnsProperty (xmlns declarations) may only be written when the current parent record is an ElementStart or the start of a complex property, array, IList, or IDictionary. Otherwise the writer throws InvalidOperationException(SR.BamlWriterBadXmlns, \"WriteXmlnsProperty\", parentType), naming the unexpected record type. BAML grammar restricts namespace declarations to these container scopes.","triggerScenarios":"Calling WriteXmlnsProperty after WriteEndDocument, directly after the document start (no element opened), after WriteDefAttribute/WriteProperty in a non-container scope, or inside a plain property scope where the top record is none of the five allowed types.","commonSituations":"Emitting xmlns mappings at the wrong nesting level in a hand-rolled serializer; writing xmlns after element content instead of at element start; state-machine bugs that leave the record stack in an unexpected type.","solutions":["Write xmlns declarations immediately after WriteStartElement for the root element, or after the corresponding *Start call for complex/array/list/dictionary scopes.","Verify the surrounding Write* call sequence forms a valid BAML element tree before adding WriteXmlnsProperty.","Log or assert PeekRecordType() before each WriteXmlnsProperty during development to catch scope mistakes early."],"exampleFix":"// before\nwriter.WriteStartDocument();\nwriter.WriteXmlnsProperty(\"x\", xamlNamespace); // no element open yet\n// after\nwriter.WriteStartDocument();\nwriter.WriteStartElement(rootType, localAssembly, assemblyVersion);\nwriter.WriteXmlnsProperty(\"x\", xamlNamespace);","handlingStrategy":"validation","validationCode":"static readonly BamlRecordType[] XmlnsScopes =\n{\n    BamlRecordType.ElementStart, BamlRecordType.PropertyComplexStart,\n    BamlRecordType.PropertyArrayStart, BamlRecordType.PropertyIListStart,\n    BamlRecordType.PropertyIDictionaryStart\n};\nbool CanWriteXmlns(BamlRecordType t) => XmlnsScopes.Contains(t);","typeGuard":null,"tryCatchPattern":"try { writer.WriteXmlnsProperty(prefix, ns); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"WriteXmlnsProperty\"))\n{\n    // log scope mismatch and skip or reposition the xmlns write\n}","preventionTips":["Emit xmlns declarations right after the element/array/list/dictionary start.","Mirror the parser's expected xmlns placement (root element, collection scopes).","Unit-test serializers with namespace-heavy XAML."],"tags":["wpf","baml","xaml-serialization","namespace","invalid-operation"],"backgroundTag":"invalid-state-transition","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"}