{"record":{"id":"cd68991aebd1e115","repo":"dotnet/wpf","slug":"sr-bamlwriternoinelement-0-1","errorCode":null,"errorMessage":"SR.BamlWriterNoInElement: {0} {1}","messagePattern":"SR\\.BamlWriterNoInElement: (.+?) (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlWriter.cs","lineNumber":303,"sourceCode":"        /// Custom serialization record.  Note that for custom serialization\n        /// to work, the assembly that contains the property's type must be \n        /// loaded. If custom serialization is not chosen, then\n        /// write out a 'normal' record, which will cause type conversion to happen\n        /// at load time from the stored string.\n        /// </remarks>\n        public void WriteProperty(\n            string             assemblyName,\n            string             ownerTypeFullName,\n            string             propName,\n            string             value,\n            BamlAttributeUsage propUsage)\n        {\n            VerifyWriteState();\n            \n            BamlRecordType parentType = PeekRecordType();\n            if (parentType != BamlRecordType.ElementStart)\n            {\n                throw new InvalidOperationException(SR.Format(SR.BamlWriterNoInElement,\n                                                           \"WriteProperty\",\n                                                           parentType.ToString()));\n            }\n\n            object dpOrPi;\n            Type   declaringType;\n            GetDpOrPi(assemblyName, ownerTypeFullName, propName, out dpOrPi, out declaringType);\n\n            // Check if the value is a MarkupExtension.  If so it must be expanded into\n            // a series of baml records.  Otherwise just write out the property.\n            AttributeData data = _extensionParser.IsMarkupExtensionAttribute(\n                                                        declaringType,    \n                                                        propName,       \n                                                    ref value,\n                                                        0,       // No line numbers for baml\n                                                        0,\n                                                        0,\n                                                        dpOrPi);","sourceCodeStart":285,"sourceCodeEnd":321,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlWriter.cs#L285-L321","documentation":"WriteProperty must be called while the writer's record stack top is an ElementStart record. When the parent record type is anything else, the writer throws InvalidOperationException(SR.BamlWriterNoInElement, \"WriteProperty\", parentType), reporting the offending record type in the message. This enforces valid BAML structure: properties can only be written directly inside an element.","triggerScenarios":"Calling WriteProperty after WriteStartElement on a nested/complex property, after WriteEndComplexProperty without re-entering an element, after WriteEndDocument, or in any state where PeekRecordType() != BamlRecordType.ElementStart.","commonSituations":"Emitting a property while inside a complex property or array/IList scope; forgetting to close and reopen an element around properties; interleaving WriteProperty and WriteStartElement incorrectly in a hand-rolled serializer.","solutions":["Ensure the last call was WriteStartElement (not WriteStartComplexProperty/array/list) before WriteProperty.","Use WriteStartComplexProperty/WriteEndComplexProperty for nested property scopes rather than attempting WriteProperty inside them.","Wrap property writes in a state check or assert that the current scope is an element."],"exampleFix":"// before\nwriter.WriteStartComplexProperty(\"Grid.Children\");\nwriter.WriteProperty(\"Width\", \"100\"); // wrong scope\n// after\nwriter.WriteStartElement(\"Button\", \"Button\", \"...\");\nwriter.WriteProperty(\"Width\", \"100\");","handlingStrategy":"try-catch","validationCode":"void EnsureElementScope(BamlWriter w, Func<BamlRecordType> peek)\n{\n    if (peek() != BamlRecordType.ElementStart)\n        throw new InvalidOperationException(\"WriteProperty requires ElementStart scope\");\n}","typeGuard":null,"tryCatchPattern":"try { writer.WriteProperty(name, value); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"WriteProperty\"))\n{\n    // recover: re-enter element scope or abort serialization\n}","preventionTips":["Track the current BAML scope in your serializer wrapper.","Only emit properties immediately after WriteStartElement.","Use separate methods for element properties vs complex property children."],"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"}