{"record":{"id":"9a571028a365a058","repo":"dotnet/wpf","slug":"found-unexpected-xmlns-baml-record","errorCode":null,"errorMessage":"Found unexpected Xmlns BAML record","messagePattern":"Found unexpected Xmlns BAML record","errorType":"exception","errorClass":"XamlParseException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/Baml2006Reader.cs","lineNumber":472,"sourceCode":"\n                case Baml2006RecordType.NamedElementStart:\n                    // This is only used by template code, and only as a temporary record, so should never occur here.\n                    throw new XamlParseException();\n\n                case Baml2006RecordType.KeyElementStart:\n                    Process_KeyElementStart();\n                    break;\n\n                case Baml2006RecordType.KeyElementEnd:\n                    Process_KeyElementEnd();\n                    break;\n                #endregion\n\n                #region Property Records\n                case Baml2006RecordType.XmlnsProperty:\n                    // This is only valid right after an ElementStart and before any other real nodes.  \n                    // Should never be seen here.\n                    throw new XamlParseException(\"Found unexpected Xmlns BAML record\");\n\n                case Baml2006RecordType.Property:\n                    Process_Property();\n                    break;\n\n                case Baml2006RecordType.PropertyCustom:\n                    Process_PropertyCustom();\n                    break;\n\n                case Baml2006RecordType.PropertyWithConverter:\n                    Process_PropertyWithConverter();\n                    break;\n\n                case Baml2006RecordType.PropertyWithExtension:\n                    Process_PropertyWithExtension();\n                    break;\n\n                case Baml2006RecordType.PropertyTypeReference:","sourceCodeStart":454,"sourceCodeEnd":490,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/Baml2006Reader.cs#L454-L490","documentation":"Baml2006Reader's record processor encountered a BAML XmlnsProperty record in a state where it is illegal — per the code comment it is 'only valid right after an ElementStart and before any other real nodes' and 'should never be seen here'. This is an internal parser state invariant: the xmlns mapping record arrived out of order relative to element records.","triggerScenarios":"Process_OneBamlRecord (via ReadKeys, ReadObject, or Process_BamlRecords) hits case Baml2006RecordType.XmlnsProperty outside the expected ElementStart context — i.e. a BAML stream whose xmlns record ordering the reader does not anticipate.","commonSituations":"Loading a corrupted or hand-modified .baml resource; a BAML file produced by a WPF version whose record layout differs from the runtime's reader; binary-mangled compiled XAML (post-build resource tampering, bad assembly merge).","solutions":["Recompile the project (rebuild) so .baml resources are regenerated by the matching WPF targets/version.","Verify the .baml resource is not corrupted: restore the original file from source control or clean/clone the repo.","Ensure the WPF assemblies (PresentationFramework) referenced at build and run time come from the same .NET version; align TargetFramework and runtime.","If you are generating or transforming BAML yourself, emit the XmlnsProperty record immediately after ElementStart and before any other real nodes."],"exampleFix":"// before (hand-crafted baml writer)\nWriteRecord(Baml2006RecordType.XmlnsProperty, xmlnsMap);\nWriteRecord(Baml2006RecordType.ElementStart, type);\n// after\nWriteRecord(Baml2006RecordType.ElementStart, type);\nWriteRecord(Baml2006RecordType.XmlnsProperty, xmlnsMap); // xmlns must immediately follow ElementStart","handlingStrategy":"try-catch","validationCode":"// Validate baml resource integrity before parsing\nbyte[] head = new byte[4];\nusing var fs = File.OpenRead(bamlPath);\nif (fs.Read(head) < 4 || head[0] != (byte)'B') throw new InvalidDataException(\"Not a BAML stream\");","typeGuard":null,"tryCatchPattern":"try\n{\n    using var reader = new Baml2006Reader(stream);\n    while (reader.Read()) { }\n}\ncatch (XamlParseException ex) when (ex.Message.Contains(\"unexpected Xmlns BAML record\"))\n{\n    // BAML record ordering invalid — rebuild/regenerate the resource\n    throw new InvalidDataException(\"Corrupt or incompatible BAML: rebuild the assembly.\", ex);\n}","preventionTips":["Never hand-edit or post-process compiled .baml resources","Rebuild cleanly after upgrading WPF/.NET target versions","Keep build SDK and runtime versions aligned","Treat BAML as a black-box binary produced only by the compiler"],"tags":["wpf","xaml","baml","baml2006reader"],"backgroundTag":"internal-invariant-violation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}