dotnet/wpf · error · XamlParseException
SR.InvalidStartOfBaml
Error message
SR.InvalidStartOfBaml
What it means
BamlResourceDeserializer.LoadBamlImp throws this XamlParseException when the first node read from the BAML stream is not a StartDocument node. Valid BAML must begin with a document start; anything else means the stream is not BAML or is corrupt.
Solutions
- Verify the stream contains compiled BAML (starts with BAML signature) not raw XAML text
- Ensure the stream Position is 0 before deserializing
- Rebuild the satellite/resource assembly to regenerate correct BAML
- Check that the assembly producing the stream and the parsing code use compatible .NET/WPF versions
Example fix
// before var deserializer = new BamlResourceDeserializer(xamlFileStream); // after xamlFileStream.Position = 0; var deserializer = new BamlResourceDeserializer(compiledBamlStream);
Defensive patterns
Strategy: validation
Validate before calling
// validate BAML magic before deserializing
var head = new byte[4]; stream.Read(head,0,4); stream.Position = 0;
if (head[0] != (byte)'B' || head[1] != (byte)'A' || head[2] != (byte)'M' || head[3] != (byte)'L') throw new IOException("Not a BAML stream"); Try / catch
try { tree = deserializer.LoadBaml(); } catch (XamlParseException ex) when (ex.Message.Contains("Baml")) { /* reload resource / rebuild assembly */ } Prevention
- Ensure stream Position is 0 before deserializing
- Verify the embedded resource is compiled BAML, not raw XAML
- Checksum resource assemblies to detect corruption
When it happens
Trigger: Deserializing a localization resource stream (via BamlLocalizer / BamlResourceDeserializer) whose bytes are not valid BAML — e.g. plain XAML text, wrong resource embedded, truncated or re-saved binary, or a stream positioned mid-file.
Common situations: Build misconfiguration embedding .xaml instead of compiled .baml; satellite assembly resource extraction bug; file corruption during copy/version mismatch between writer and reader; forgetting to seek the stream to position 0.
Understand the failure class
Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.
Related errors
- SR.InvalidEndOfBaml
- SR.UnRecognizedBamlNodeType
- Can't Assign to Known Type attributes
- Could not find prefix for type
- Found unexpected Xmlns BAML record
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/3cb37554a92bd29c.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Globalization/BamlResourceDeserializer.cs:52
{
}
//-----------------------------
// private methods
//-----------------------------
/// <summary>
/// build the baml element tree as well as the localizability inheritance tree
/// </summary>
/// <param name="bamlSteam">input baml stream.</param>
/// <returns>the tree constructed.</returns>
private BamlTree LoadBamlImp(Stream bamlSteam)
{
_reader = new BamlReader(bamlSteam);
_reader.Read();
if (_reader.NodeType != BamlNodeType.StartDocument)
{
throw new XamlParseException(SR.InvalidStartOfBaml);
}
// create root element.
_root = new BamlStartDocumentNode();
PushNodeToStack(_root);
// A hash table used to handle duplicate properties within an element
Hashtable propertyOccurrences = new Hashtable(8);
// create the tree by depth first traversal.
while (_bamlTreeStack.Count > 0 && _reader.Read())
{
switch (_reader.NodeType)
{
case BamlNodeType.StartElement:
{
BamlTreeNode bamlNode = new BamlStartElementNode(
_reader.AssemblyName,View on GitHub (pinned to 81131a70a4)