Every layout here is read from WPF’s own source (BamlRecords.cs, BamlVersionHeader.cs, Baml2006Reader.cs and Baml2006RecordType.cs in dotnet/wpf) and checked against real files: two windows compiled for this site and 225 files taken from the assemblies of MaterialDesignThemes, ModernWpfUI and Microsoft.Xaml.Behaviors.Wpf, 224,882 records, all of which read to their DocumentEnd with every record size matching its fields.
Numbers and strings
- All numbers are little-endian.
Int16is a signed 16-bit number (an id),Int32a signed 32-bit one,UInt16andUInt32the unsigned forms. - A 7-bit encoded number (
Read7BitEncodedIntof .NET’s BinaryReader) takes seven bits of the number to a byte, the low group first; a set high bit says another byte follows. At most five bytes. - A string is a 7-bit encoded length in bytes, then that many bytes of UTF-8.
- A boolean is one byte, 0 or 1.
- The size that starts a variable-size record is a 7-bit encoded number that counts its own bytes and everything after it in the record, not the record type byte. A record that ends before or after its size says is a fault; the viewer shows the extra bytes and lists the problem.
The header
The header is 28 bytes: a FormatVersion.
| Offset | Size | Field | Meaning |
|---|---|---|---|
| 0 | 4 | signature length | Int32, 12: the length in bytes of the signature. |
| 4 | 12 | signature | the six characters MSBAML in UTF-16 (4D 00 53 00 42 00 41 00 4D 00 4C 00). |
| 16 | 4 | reader version | UInt16 major, UInt16 minor: the oldest reader that can read the stream. |
| 20 | 4 | updater version | the same: the oldest writer that can update it. |
| 24 | 4 | writer version | the same: the version of the writer. |
WPF writes 0.96 in all three (00 00 60 00 each) and its reader refuses a stream whose reader version is not 0.96. All 227 files read have 0.96. The first record follows at byte 28.
The records
A record is a one-byte type, then the fields below. Size means the 7-bit encoded size described above. Type id, property id, string id, assembly id and converter id are the ids of the tables.
| Type | Name | Fields |
|---|---|---|
| 1 | DocumentStart | LoadAsync (boolean), MaxAsyncRecords (Int32, -1 in compiled files), DebugBaml (boolean) |
| 2 | DocumentEnd | none; the stream ends here |
| 3 | ElementStart | type id (Int16), flags (byte): 1 = created with a type converter, 2 = the object is retrieved (GetObject) from its parent’s property instead of being created |
| 4 | ElementEnd | none |
| 5 | Property | size, property id (Int16), value (string) |
| 6 | PropertyCustom | size, property id (Int16), serializer type id (Int16; bit 0x4000 set means the data names a member of a type), then the data up to the end of the record: the compact value |
| 7 | PropertyComplexStart | property id (Int16): a property element whose value is one object |
| 8 | PropertyComplexEnd | none |
| 9 | PropertyArrayStart | property id (Int16): a property element that holds an array |
| 10 | PropertyArrayEnd | none |
| 11 | PropertyIListStart | property id (Int16): a collection (IList) |
| 12 | PropertyIListEnd | none |
| 13 | PropertyIDictionaryStart | property id (Int16): a dictionary |
| 14 | PropertyIDictionaryEnd | none |
| 15 | LiteralContent | size, value (string; the text of an x:XData), line number (Int32), line position (Int32) |
| 16 | Text | size, value (string) |
| 17 | TextWithConverter | size, value (string), converter type id (Int16) |
| 18 | RoutedEvent | size, property id (Int16), value (string) |
| 19 | ClrEvent | no layout; WPF’s reader throws NotImplementedException |
| 20 | XmlnsProperty | size, prefix (string), XML namespace (string), assembly count (Int16), that many assembly ids (Int16 each) |
| 21 | XmlAttribute | no layout (not implemented) |
| 22 | ProcessingInstruction | no layout (not implemented) |
| 23 | Comment | no layout (not implemented) |
| 24 | DefTag | no layout (not implemented) |
| 25 | DefAttribute | size, value (string), name string id (Int16): an attribute of the XAML language namespace, such as x:Uid or x:Name (the name is a string of the table) |
| 26 | EndAttributes | no layout (not implemented) |
| 27 | PIMapping | size, XML namespace (string), CLR namespace (string), assembly id (Int16): the clr-namespace: mapping of a prefix |
| 28 | AssemblyInfo | size, assembly id (Int16), full name (string) |
| 29 | TypeInfo | size, type id (Int16), assembly id (Int16; the high four bits are flags: 1 = internal type), full type name (string) |
| 30 | TypeSerializerInfo | as TypeInfo, then a serializer type id (Int16, not used by the reader) |
| 31 | AttributeInfo | size, property id (Int16), declaring type id (Int16), usage (byte: 0 default, 1 xml:lang, 2 xml:space, 3 runtime name), name (string) |
| 32 | StringInfo | size, string id (Int16), value (string) |
| 33 | PropertyStringReference | property id (Int16), string id (Int16) |
| 34 | PropertyTypeReference | property id (Int16), type id (Int16): {x:Type …} as the value |
| 35 | PropertyWithExtension | property id (Int16), extension id (Int16: the low 12 bits name the markup extension type as a built-in type id with its sign dropped; 0x4000 = the value is a type, 0x2000 = the value is a static member), value id (Int16) |
| 36 | PropertyWithConverter | size, property id (Int16), value (string), converter type id (Int16) |
| 37 | DeferableContentStart | content size (Int32): the number of bytes of the values that follow the keys of a dictionary |
| 38 | DefAttributeKeyString | size, key string id (Int16), value position (Int32), shared (boolean), shared set (boolean): the x:Key of a dictionary item; the position is where the item’s value starts, counted from the end of the keys |
| 39 | DefAttributeKeyType | type id (Int16), flags (byte), value position (Int32), shared (boolean), shared set (boolean): x:Key="{x:Type …}" |
| 40 | KeyElementStart | the same fields as 39: a key written as an element; its own records follow up to 41 |
| 41 | KeyElementEnd | none |
| 42 | ConstructorParametersStart | none: the arguments of a constructor follow, as the XAML 2009 x:Arguments |
| 43 | ConstructorParametersEnd | none |
| 44 | ConstructorParameterType | type id (Int16): the type of the next argument |
| 45 | ConnectionId | connection id (Int32): the number the generated Connect method uses to attach an event handler or a named field to the element |
| 46 | ContentProperty | property id (Int16): the content property of the element, named so that the children that follow are its value |
| 47 | NamedElementStart | type id (Int16), runtime name (string); only a temporary record of template processing, which the reader refuses in a final file |
| 48 | StaticResourceStart | type id (Int16), flags (byte), as ElementStart; an object that stands for a {StaticResource} in the keys of a dictionary item |
| 49 | StaticResourceEnd | none |
| 50 | StaticResourceId | static resource id (Int16): the index in the list of static resources of the dictionary item being read |
| 51 | TextWithId | size, string id (Int16): text that is a string of the table |
| 52 | PresentationOptionsAttribute | size, value (string), name string id (Int16): the po:Freeze directive |
| 53 | LineNumberAndPosition | line number (Int32), line position (Int32): debug information |
| 54 | LinePosition | line position (Int32) |
| 55 | OptimizedStaticResource | flags (byte: 1 = the key is a type, 2 = the key is a static member, 0 = the key is a string), value id (Int16): a {StaticResource key} written compactly |
| 56 | PropertyWithStaticResourceId | property id (Int16), static resource id (Int16): the property’s value is that static resource |
How the records are ordered
- The table records (
AssemblyInfo,TypeInfo,AttributeInfo,StringInfo) are written just before the first record that uses the entry they define, so a table grows while the stream is read;PIMappingandXmlnsPropertyfollow theElementStartof the element that declares the namespace. - After an
ElementStartcome itsXmlnsPropertyandLinePositionrecords, its attribute-like records (Property,PropertyCustom,PropertyWithConverter,PropertyWithExtension,PropertyTypeReference,DefAttribute,ConnectionIdand the others), thenContentPropertyand the content, thenElementEnd. A property element is aPropertyComplexStart,PropertyArrayStart,PropertyIListStartorPropertyIDictionaryStartrecord, its content, and the matching end record. - A
ResourceDictionarywith items writesDeferableContentStart, then every item’s key record (DefAttributeKeyString,DefAttributeKeyTypeor aKeyElementStartblock), each followed by theOptimizedStaticResourceandStaticResourceStartrecords of the resources that item uses, and then the items themselves in the same order. AStaticResourceIdin an item counts through the list of the item’s own static resources. - The records nest like the markup: a start record raises the nesting and its end record lowers it.
ElementStart, the four property-start records,KeyElementStart,NamedElementStart,StaticResourceStartandConstructorParametersStartopen a level;ElementEnd, the four property-end records,KeyElementEnd,StaticResourceEndandConstructorParametersEndclose one.