dotnet/wpf · error · XamlParseException
Found unexpected Xmlns BAML record
Error message
Found unexpected Xmlns BAML record
What it means
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.
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.
Example fix
// before (hand-crafted baml writer) WriteRecord(Baml2006RecordType.XmlnsProperty, xmlnsMap); WriteRecord(Baml2006RecordType.ElementStart, type); // after WriteRecord(Baml2006RecordType.ElementStart, type); WriteRecord(Baml2006RecordType.XmlnsProperty, xmlnsMap); // xmlns must immediately follow ElementStart
Defensive patterns
Strategy: try-catch
Validate before calling
// Validate baml resource integrity before parsing
byte[] head = new byte[4];
using var fs = File.OpenRead(bamlPath);
if (fs.Read(head) < 4 || head[0] != (byte)'B') throw new InvalidDataException("Not a BAML stream"); Try / catch
try
{
using var reader = new Baml2006Reader(stream);
while (reader.Read()) { }
}
catch (XamlParseException ex) when (ex.Message.Contains("unexpected Xmlns BAML record"))
{
// BAML record ordering invalid — rebuild/regenerate the resource
throw new InvalidDataException("Corrupt or incompatible BAML: rebuild the assembly.", ex);
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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).
Understand the failure class
Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.
Related errors
- Can't Assign to Known Type attributes
- Could not find prefix for type
- SR.ExpectedBamlSchemaContext
- SR.Format(SR.UnknownBamlRecord, recordType)
- SR.InvalidDeSerialize
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/9a571028a365a058.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/Baml2006Reader.cs:472
case Baml2006RecordType.NamedElementStart:
// This is only used by template code, and only as a temporary record, so should never occur here.
throw new XamlParseException();
case Baml2006RecordType.KeyElementStart:
Process_KeyElementStart();
break;
case Baml2006RecordType.KeyElementEnd:
Process_KeyElementEnd();
break;
#endregion
#region Property Records
case Baml2006RecordType.XmlnsProperty:
// This is only valid right after an ElementStart and before any other real nodes.
// Should never be seen here.
throw new XamlParseException("Found unexpected Xmlns BAML record");
case Baml2006RecordType.Property:
Process_Property();
break;
case Baml2006RecordType.PropertyCustom:
Process_PropertyCustom();
break;
case Baml2006RecordType.PropertyWithConverter:
Process_PropertyWithConverter();
break;
case Baml2006RecordType.PropertyWithExtension:
Process_PropertyWithExtension();
break;
case Baml2006RecordType.PropertyTypeReference:View on GitHub (pinned to 81131a70a4)