dotnet/wpf · error · InvalidOperationException

SR.ParserUnknownBaml

Error message

SR.ParserUnknownBaml: {0}

What it means

ReadNextRecord dispatches on the current BAML record type; the switch's default case exists because an unrecognized BamlRecordType cannot appear in a well-formed stream. Encountering one means the BAML is corrupt or was produced by a newer compiler whose record types this reader does not understand, so it throws InvalidOperationException(SR.ParserUnknownBaml) with the numeric record type.

Solutions

  1. Recompile the BAML/XAML with the markup compiler matching the runtime version loading it.
  2. Ensure the runtime framework version is >= the version that produced the BAML.
  3. Restore/replace the corrupted assembly or .baml resource.
  4. If writing tooling, validate record boundaries before dispatch and log the unknown RecordType value instead of assuming valid input.

Example fix

// before
// loading BAML built with newer-framework record types on an older runtime
// after
// rebuild the XAML with the target framework's PresentationBuildTasks so emitted record types match the reader
Defensive patterns

Strategy: try-catch

Validate before calling

// No pre-call validation possible for stream contents; verify version instead:
// ensure the assembly containing the BAML was built with a markup compiler <= the runtime version.

Try / catch

try { while (reader.Read()) { } }
catch (InvalidOperationException ex) when (ex.Message.StartsWith("ParserUnknownBaml") || ex.Message.Contains("record"))
{
    // BAML from an incompatible/newer compiler or corrupt; recompile or reload resource
}

Prevention

When it happens

Trigger: Parsing a BAML stream containing a record type absent from the reader's switch — typically BAML compiled by a newer .NET/WPF version than the runtime parsing it, or a corrupted/truncated stream misaligned on record boundaries.

Common situations: Version skew: precompiled BAML (from a newer framework or third-party tool) loaded by an older runtime; manually edited or damaged assembly resources; fuzzing/custom BAML generators emitting invalid record types.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlReader.cs:657

                        ReadConstructorStart();
                        break;

                    case BamlRecordType.ConstructorParametersEnd:
                        ReadConstructorEnd();
                        break;

                    case BamlRecordType.ConnectionId:
                        ReadConnectionIdRecord();
                        break;

                    case BamlRecordType.StaticResourceId:
                        ReadStaticResourceId();
                        keepOnReading = true;
                        break;

                    default:
                        // Can't have any other type of record at this point.
                        throw new InvalidOperationException(SR.Format(SR.ParserUnknownBaml,
                                         ((int)_currentBamlRecord.RecordType).ToString(CultureInfo.CurrentCulture)));
                }
            }
        }

        /***************************************************************************\
        *
        * BamlReader.ReadProperties
        *
        * This is called when an element start has been encountered (or something
        * similar) and a number of properties may or may not follow.  Read all the
        * properties, storing them in the _properties arraylist.  When a non-property
        * record is encountered, stop, but store that record as the
        * _currentBamlRecord.
        *
        \***************************************************************************/

        private void ReadProperties()

View on GitHub (pinned to 81131a70a4)