Format:BAML
| On this wiki | Facts · Code · Notes · Topics |
|---|
Binary XAML of WPF (compiled .baml files inside the <App>.g.resources of a .NET assembly, URI form /Window1.baml): a header with the UTF-16 signature MSBAML and version 0.0x60, followed by a stream of records identified by a one-byte record type (DocumentStart, ElementStart, TypeInfo, AttributeInfo, Property ...).
WPF compiles each .xaml file to .baml and embeds it as an entry in the <AssemblyName>.g.resources managed resource (Format:.NET resources (.resources)); the entry name is the lower-case relative path, e.g. views/mainwindow.baml. At run time Baml2006Reader replays the records into XAML nodes. A decompiler recreates XAML text from them. The code-behind and event handlers are ordinary IL (Format:.NET metadata (ECMA-335)); BAML only names them, so it shows which control calls which method.
Header
uint32 signatureLength (12, in bytes), the UTF-16 string MSBAML (12 bytes: 4D 00 53 00 42 00 41 00 4D 00 4C 00), padding to a multiple of 4, then three versions (reader, updater, writer) as uint16 major, uint16 minor, each 0.0x60 in files ILSpy accepts: 00 00 60 00 x3 (Fact:BAML/struct/Header). The WPF reader skips length + 12 bytes without checking.
Records
After the header the file is a sequence of records: one byte record type, then the payload. Records that are "sized" start the payload with a 7-bit encoded int that is the size of the record including that size field itself (so a reader that does not know a record can skip it). Fixed records (ElementStart, ElementEnd, ...) have a fixed layout. Record type values are in Fact:BAML/enum/Record types: 1 DocumentStart, 2 DocumentEnd, 3 ElementStart, 4 ElementEnd, 5 Property, 6 PropertyCustom, 7/8 PropertyComplexStart/End, 9/10 PropertyArrayStart/End, 11/12 PropertyIListStart/End, 13/14 PropertyIDictionaryStart/End, 15 LiteralContent, 16 Text, 17 TextWithConverter, 18 RoutedEvent, 19 ClrEvent, 20 XmlnsProperty, 25 DefAttribute, 27 PIMapping, 28 AssemblyInfo, 29 TypeInfo, 30 TypeSerializerInfo, 31 AttributeInfo, 32 StringInfo, 33 PropertyStringReference, 34 PropertyTypeReference, 35 PropertyWithExtension, 36 PropertyWithConverter, 37 DeferableContentStart, 38 DefAttributeKeyString, 39 DefAttributeKeyType, 40/41 KeyElementStart/End, 42-44 ConstructorParameters Start/End/Type, 45 ConnectionId, 46 ContentProperty, 47 NamedElementStart, 48-50 StaticResource Start/End/Id, 51 TextWithId, 52 PresentationOptionsAttribute, 53 LineNumberAndPosition, 54 LinePosition, 55 OptimizedStaticResource, 56 PropertyWithStaticResourceId.
Tables built by the info records
The stream first declares ids that later records refer to (Fact:BAML/struct/Info records):
- AssemblyInfo (28):
uint16 assemblyId, string assembly full name. - TypeInfo (29):
uint16 typeId,uint16 assemblyId, string type full name. In the WPF reader the top 4 bits of the assembly field are flags (0x1 = internal type) and the low 12 bits the assembly id; ILSpy keeps the field whole. - AttributeInfo (31):
uint16 attributeId,uint16 ownerTypeId,uint8 usage, string property name. - StringInfo (32):
uint16 id, string value.
ElementStart (3) is int16 typeId; ConnectionId (45) is int32 and links an element to the generated Connect(int, object) event hookup in code. Type ids and attribute ids below a fixed limit denote WPF's built-in known types and properties (a table compiled into PresentationFramework, KnownTypes / KnownMembers in ILSpy): a negative id in the stream means a known type, so TypeInfo is only written for types outside the table. Unverified: exact sign convention and the boundary; read ILSpy Baml/KnownThings*.cs.
Tools
ILSpy (ICSharpCode.BamlDecompiler) shows the XAML for .baml resources; dnSpyEx has a similar one. For a custom parser, BamlReader.cs and BamlRecords.cs in ILSpy are a compact reference with write support.
See also Tool:ILSpy.
Code & repositories
- https://github.com/dotnet/wpf @
65d15adece1df9a73e3d1abc11f8bbf99e9fc7a9· license: MIT
WPF (MIT): Baml2006Reader and Baml2006RecordType under PresentationFramework/System/Windows/Markup/Baml2006.
Tested on: source read, not run: Read the source at this commit; nothing run. · checked 2026-10-18; last activity 2026-10-15 - https://github.com/icsharpcode/ILSpy @
0abb04db61517e0564923a7beadbab4da13fd830· license: MIT (read from LICENSE; GitHub's API was busy)
ILSpy (MIT): ICSharpCode.BamlDecompiler, a BAML reader/writer (Baml/BamlReader.cs, BamlRecords.cs) and XAML decompiler.
Tested on: source read, not run: Read the source at this commit; nothing run. · checked 2026-10-18
Evidence & references
- dotnet/wpf
src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/:Baml2006RecordType.cs,Baml2006Reader.cs(Process_Header, Process_AssemblyInfo/TypeInfo/AttributeInfo/StringInfo, Read_RecordSize). - icsharpcode/ILSpy
ICSharpCode.BamlDecompiler/Baml/BamlReader.cs,BamlRecords.cs.