{"record":{"id":"322e5ab8cea887c4","repo":"dotnet/wpf","slug":"sr-bamlwriterstartdoc","errorCode":null,"errorMessage":"SR.BamlWriterStartDoc","messagePattern":"SR\\.BamlWriterStartDoc","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlWriter.cs","lineNumber":149,"sourceCode":"\n#region Record Writing\n\n        /// <summary>\n        /// Write the start of document record, giving the baml version string\n        /// </summary>\n        /// <remarks>\n        /// This must be the first call made when creating a new baml file.  This\n        /// is needed to specify the start of the document and baml version.\n        /// </remarks>\n        public void WriteStartDocument()\n        {\n            if (_closed)\n            {\n                throw new InvalidOperationException(SR.BamlWriterClosed);\n            }\n            if (_startDocumentWritten)\n            {\n                throw new InvalidOperationException(SR.BamlWriterStartDoc);\n            }\n            \n            XamlDocumentStartNode node = new XamlDocumentStartNode(0,0,_depth);\n            _bamlRecordWriter.WriteDocumentStart(node);\n            _startDocumentWritten = true;\n            Push(BamlRecordType.DocumentStart);\n        }\n\n        /// <summary>\n        /// Write the end of document record.\n        /// </summary>\n        /// <remarks>\n        /// This must be the last call made when creating a new baml file.\n        /// </remarks>\n        public void WriteEndDocument()\n        {\n            VerifyEndTagState(BamlRecordType.DocumentStart, \n                              BamlRecordType.DocumentEnd);","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlWriter.cs#L131-L167","documentation":"BamlWriter.WriteStartDocument throws this InvalidOperationException when WriteStartDocument is called a second time. BAML output must begin with exactly one document-start record, so the writer tracks _startDocumentWritten and rejects duplicate calls. It is a caller sequencing bug, not a data problem.","triggerScenarios":"Calling WriteStartDocument() twice on the same BamlWriter instance, e.g. calling it again after an exception-and-retry inside Serialize, or reusing a writer object believed to be fresh.","commonSituations":"Wrapping Serialize in retry logic that restarts mid-stream without recreating the BamlWriter; copying sample code that calls WriteStartDocument when the serializer already did; accidental double invocation in custom serialization loops.","solutions":["Call WriteStartDocument at most once per BamlWriter instance; guard with a flag or assert before calling.","If retrying serialization after failure, create a new BamlWriter (and underlying stream) instead of restarting the old one.","Check for duplicate Serialize entry points (e.g. both an explicit WriteStartDocument and a Serialize call that writes it)."],"exampleFix":"// before\nwriter.WriteStartDocument();\nSerialize(writer, obj); // Serialize also writes start document\n// after\nSerialize(writer, obj); // let Serialize own the document lifecycle","handlingStrategy":"validation","validationCode":"bool startDocWritten = false;\nvoid SafeWriteStartDocument(BamlWriter w)\n{\n    if (!startDocWritten) { w.WriteStartDocument(); startDocWritten = true; }\n}","typeGuard":null,"tryCatchPattern":"try { writer.WriteStartDocument(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"start document\")) { /* already started; ignore */ }","preventionTips":["Let Serialize own WriteStartDocument; never call it manually alongside Serialize.","Recreate BamlWriter per serialization attempt instead of restarting a used one.","Assert document lifecycle invariants in debug builds."],"tags":["wpf","baml","xaml-serialization","invalid-operation"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}