dotnet/wpf · error · XamlParseException

SR.Format(SR.UnknownBamlRecord, recordType)

Error message

SR.Format(SR.UnknownBamlRecord, recordType)

What it means

The reader encountered a BAML record type it does not know (Baml2006RecordType.Unknown or an unhandled value) and throws XamlParseException wrapping SR.UnknownBamlRecord with the numeric record type. It means the BAML stream contains a record the loaded Wpf Xaml parser cannot interpret.

Solutions

  1. Update the runtime to a .NET/WPF version at least as new as the toolchain that compiled the BAML (align TargetFramework and installed runtime).
  2. Rebuild the application so BAML is regenerated by the current SDK instead of loading stale compiled resources.
  3. Check for resource corruption — compare the .baml bytes against a clean build.
  4. If implementing custom BAML tooling, restrict emitted records to types known to the target Baml2006Reader version.

Example fix

// before: csproj targets net472 but baml was compiled with net8.0 toolchain and deployed to net472
<TargetFramework>net472</TargetFramework>
// after
<TargetFramework>net8.0</TargetFramework> <!-- match the SDK that produced the baml -->
Defensive patterns

Strategy: try-catch

Validate before calling

// Ensure runtime is not older than the toolchain that compiled resources
var runtime = Environment.Version;
if (runtime.Major < targetCompiledMajor) throw new PlatformNotSupportedException("BAML compiled by newer SDK");

Try / catch

try
{
    using var reader = new Baml2006Reader(stream);
    while (reader.Read()) { }
}
catch (XamlParseException ex) when (ex.Message.Contains("Unknown BAML record"))
{
    // recordType number is embedded in the message; log it and advise upgrade
    logger.LogError(ex, "Unknown BAML record — runtime/toolchain version mismatch");
    throw;
}

Prevention

When it happens

Trigger: Process_OneBamlRecord's switch (reached via ReadKeys, ReadObject, or Process_BamlRecords) falls into the default case because recordType is not one of the known Baml2006RecordType values.

Common situations: Loading BAML compiled by a newer .NET/WPF version than the runtime parsing it (forward-compatibility gap); corrupted resource stream; a tool rewrote record bytes and shifted the type codes.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/622d797894134954. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/Baml2006Reader.cs:672

                    break;

                case Baml2006RecordType.LineNumberAndPosition:
                    Process_LineNumberAndPosition();
                    break;

                case Baml2006RecordType.Comment:
                    Process_Comment();
                    break;

                #endregion

                case Baml2006RecordType.ConnectionId:
                    Process_ConnectionId();
                    break;

                case Baml2006RecordType.Unknown:
                default:
                    throw new XamlParseException(string.Format(CultureInfo.CurrentCulture, SR.Format(SR.UnknownBamlRecord, recordType)));
            }

            return true;
        }

        // Have not seen a BAML file that has this...
        private void Process_ProcessingInstruction()
        {
            throw new NotImplementedException();
        }

        // Have not seen a BAML file that has this...
        private void Process_DefTag()
        {
            throw new NotImplementedException();
        }

        // Have not seen a BAML file that has this...

View on GitHub (pinned to 81131a70a4)