Introduction
BAML (Binary Application Markup Language) is the compiled form of a XAML file in WPF. The build’s markup compiler reads each Page and ApplicationDefinition XAML file and writes a .baml stream that it embeds in the assembly, in the AssemblyName.g.resources resource, under a lower-case name such as mainwindow.baml. At run time InitializeComponent loads that stream, so no XML is parsed and no type is looked up by name: the strings, type names and property names are replaced by small numbers that index tables written into the stream itself.
A .baml file is rarely met alone: it is extracted from an assembly by decompilers and resource tools, or it is kept as a build output (obj/Debug/net10.0-windows/MainWindow.baml). The format has the version 0.96 in the reader, updater and writer fields, and has not changed in the .NET Framework 4 and .NET (Core) versions of WPF.
Structure
- Header: 28 bytes
- DocumentStart
- AssemblyInfo, TypeInfo, AttributeInfo and StringInfo records, each just before the first record that uses it
- ElementStart, Property… and the other records of the element tree
- DocumentEnd
The header is the length 12 as a 32-bit number, the six UTF-16 characters MSBAML, and the reader, updater and writer versions as two 16-bit numbers each (major, minor). The records follow, each a one-byte type and then its fields; a record with strings starts with its size as a 7-bit encoded number. There are 56 record types, of which 50 have a layout that WPF’s compiler writes; they are tabulated with every field on the header and records page.
The tree is written in document order. An ElementStart record names the type of the object by a type id; the records that follow, until its ElementEnd, are its namespace declarations, its properties (as attributes, as property elements that hold their own records, or as compact custom values), its content and its children. A resource dictionary writes its keys first, then its values (the DeferableContentStart record), so that a value is created only when it is asked for; every key carries the position of its value. Events are not in the stream: an element that has an event handler or a name gets a ConnectionId record, and the generated Connect method links that number to the handler.
The ids and tables page tells how an id is read: a negative id names one of the 759 types, 268 properties or 2 strings built into WPF’s reader, a number of 0 or more names an entry of the file’s own tables, and the compact values (brushes, path data, point lists, number lists) are described there.
How the viewer reads a file
The viewer reads every record and keeps its byte range, so the tree shows the file in byte order with every field of every record, and no byte is left without a node. The Records tab lists every record with what it means: the type, property or string an id stands for, the converter, the value of a custom record, and the nesting by indent. The XAML tab rebuilds an approximate XAML text from the records, to be read next to the original; it is not the original markup (events, comments and the order of attributes are not in the file, and an attribute is written with its owner type only for the usual attached properties). The Tables tab shows the assemblies, types, properties and strings the file defines. Every problem found (a record size that disagrees with its fields, a record type with no layout, bytes after the end, an element never closed) is listed.
Nothing is loaded and nothing is run: a type is only a name, and the meaning of an enumeration value or a type converter’s result is not looked up, because the assemblies are not available.
The pages
- Header and records – the header, the encoding of numbers and strings, and the layout of every record type
- Ids, tables and compact values – type, property, string and assembly ids, the built-in tables, extensions, static resources and the compact binary values
- Compilation, code-behind and BAML – how the build produces the stream, on the XAML pages
Not described
Known, not yet covered
- Viewer: the names of the members of an enumeration (a
PropertyCustomenum value is shown as its number, aPropertyWithConvertervalue as its text) and the result of a type converter are not looked up: the assemblies that define them are not read. The number is labelled with where its meaning comes from. - Viewer: the
.g.resourcesstream that holds the .baml files of an assembly is not opened by this plugin, so a .baml inside an assembly has to be extracted first (the .NET assembly view lists the resource; the extraction is not offered). - Reference: the BAML of .NET Framework 3.0 and 3.5 (the older record set of
BamlMapTable, with its own known-type numbers) is not described; version 0.96 is the version of every .NET Framework 4 and .NET (Core) file. - Reference: the records
ClrEvent,XmlAttribute,ProcessingInstruction,Comment,DefTagandEndAttributesare named in the record list, but WPF’s reader and writer give them no layout (the reader throws NotImplementedException), so the viewer stops at one and shows the rest as unread bytes.
Unknown
- Reference: the records
RoutedEvent,TypeSerializerInfo,PropertyStringReference,NamedElementStart,StaticResourceStart,StaticResourceEnd,StaticResourceId,TextWithId,LineNumberAndPositionandLinePositionare read from the source of WPF’sBamlRecords.csandBaml2006Reader.cs; no real file available here contains them (the 227 files read, from three libraries and two windows compiled for this site, hold the other types, 224,882 records in all, none of them damaged), so their layouts are unconfirmed by a real stream. - Reference: the numbering of the system resource keys (
x:StaticofSystemColors,SystemFonts,SystemParameters) comes from the order of theSystemResourceKeyIDenumeration in the source; it agrees with the keys in the real files read, but the owner class of a theme style key of a control (MenuItemSeparatorStyleand the like) is not resolved.
Links
Software
- ILSpy – decompiler whose BAML decompiler turns .baml back into XAML (Windows, macOS, Linux)
- Visual Studio – builds the BAML of a WPF project (Windows)
Developers
- Baml2006Reader – the .NET class that reads BAML as a stream of XAML nodes (C#)
- dotnet/wpf, System.Windows.Markup – the source of the records, the reader and the writer (C#)
Reference
- XAML – the markup that BAML compiles (on this site)
- Compilation, code-behind and BAML – the build steps around the stream (on this site)