{"record":{"id":"3cb37554a92bd29c","repo":"dotnet/wpf","slug":"sr-invalidstartofbaml","errorCode":null,"errorMessage":"SR.InvalidStartOfBaml","messagePattern":"SR\\.InvalidStartOfBaml","errorType":"exception","errorClass":"XamlParseException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Globalization/BamlResourceDeserializer.cs","lineNumber":52,"sourceCode":"        {\n        }\n\n        //-----------------------------\n        // private  methods\n        //-----------------------------\n        /// <summary>\n        /// build the baml element tree as well as the localizability inheritance tree\n        /// </summary>\n        /// <param name=\"bamlSteam\">input baml stream.</param>\n        /// <returns>the tree constructed.</returns>\n        private BamlTree LoadBamlImp(Stream bamlSteam)\n        {\n            _reader = new BamlReader(bamlSteam);\n            _reader.Read();\n\n            if (_reader.NodeType != BamlNodeType.StartDocument)\n            {\n                throw new XamlParseException(SR.InvalidStartOfBaml);\n            }\n\n            // create root element.\n            _root = new BamlStartDocumentNode();\n            PushNodeToStack(_root);\n\n            // A hash table used to handle duplicate properties within an element\n            Hashtable propertyOccurrences = new Hashtable(8);\n\n            // create the tree by depth first traversal.\n            while (_bamlTreeStack.Count > 0 && _reader.Read())\n            {\n                switch (_reader.NodeType)\n                {\n                    case BamlNodeType.StartElement:\n                        {\n                            BamlTreeNode bamlNode = new BamlStartElementNode(\n                                _reader.AssemblyName,","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Globalization/BamlResourceDeserializer.cs#L34-L70","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nvar deserializer = new BamlResourceDeserializer(xamlFileStream);\n// after\nxamlFileStream.Position = 0;\nvar deserializer = new BamlResourceDeserializer(compiledBamlStream);","handlingStrategy":"validation","validationCode":"// validate BAML magic before deserializing\nvar head = new byte[4]; stream.Read(head,0,4); stream.Position = 0;\nif (head[0] != (byte)'B' || head[1] != (byte)'A' || head[2] != (byte)'M' || head[3] != (byte)'L') throw new IOException(\"Not a BAML stream\");","typeGuard":null,"tryCatchPattern":"try { tree = deserializer.LoadBaml(); } catch (XamlParseException ex) when (ex.Message.Contains(\"Baml\")) { /* reload resource / rebuild assembly */ }","preventionTips":["Ensure stream Position is 0 before deserializing","Verify the embedded resource is compiled BAML, not raw XAML","Checksum resource assemblies to detect corruption"],"tags":["baml","xaml","localization","corrupt-input"],"backgroundTag":"schema-validation-failed","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}