{"record":{"id":"f1afe9c7c8ae42be","repo":"dotnet/wpf","slug":"invalidoperationexception-sr-format-sr-f1afe9","errorCode":null,"errorMessage":"InvalidOperationException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteEndObject\"))","messagePattern":"InvalidOperationException\\(SR\\.Format\\(SR\\.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteEndObject\"\\)\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs","lineNumber":1092,"sourceCode":"                    }\n\n                    writer.ppStateInfo.ReturnState = State;\n                    writer.currentState = ExpandPositionalParameters.State;\n                }\n                else\n                {\n                    writer.currentState = InMember.State;\n                }\n            }\n\n            public override void WriteEndObject(XamlXmlWriter writer)\n            {\n                Debug.Assert(writer.namespaceScopes.Count > 0);\n                Frame frame = writer.namespaceScopes.Pop();\n                if (frame.AllocatingNodeType != XamlNodeType.StartObject &&\n                    frame.AllocatingNodeType != XamlNodeType.GetObject)\n                {\n                    throw new InvalidOperationException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteEndObject\"));\n                }\n\n                if (!frame.IsObjectFromMember)\n                {\n                    writer.output.WriteEndElement();\n                }\n\n                if (writer.namespaceScopes.Count > 0)\n                {\n                    writer.currentState = InMemberAfterEndObject.State;\n                }\n                else\n                {\n                    writer.Flush();\n                    writer.currentState = End.State;\n                }\n            }\n        }","sourceCodeStart":1074,"sourceCodeEnd":1110,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs#L1074-L1110","documentation":"On WriteEndObject the writer pops the current namespace-scope frame and validates that the frame was created by a StartObject or GetObject node. If the frame's AllocatingNodeType is something else (e.g. created for a member or value), the object being ended does not correspond to an object start, so it throws InvalidOperationException 'WriteEndObject not supported in current state'. This protects the element nesting of the emitted XML.","triggerScenarios":"Calling XamlXmlWriter.WriteEndObject when the innermost frame was allocated by WriteStartMember/WriteValue rather than WriteStartObject/WriteGetObject — i.e. mismatched start/end pairs.","commonSituations":"Hand-rolled node loops where an EndObject is emitted after a member without closing the member first; error-recovery code that skips starts but still emits ends; duplicated WriteEndObject calls.","solutions":["Ensure every WriteEndObject matches a prior WriteStartObject or WriteGetObject at the same frame level","End any open members (WriteEndMember) before WriteEndObject","Log/balance node emissions in the writing loop to detect mismatches early"],"exampleFix":"// before\nwriter.WriteStartMember(m);\nwriter.WriteEndObject(); // wrong frame -> throws\n// after\nwriter.WriteStartMember(m);\nwriter.WriteEndMember();\nwriter.WriteEndObject();","handlingStrategy":"validation","validationCode":"if (openFrameAllocatedBy != \"StartObject\" && openFrameAllocatedBy != \"GetObject\") throw new InvalidOperationException(\"WriteEndObject requires an object frame\");","typeGuard":"bool CanWriteEndObject(XamlNodeType frameAllocType) => frameAllocType is XamlNodeType.StartObject or XamlNodeType.GetObject;","tryCatchPattern":"try { writer.WriteEndObject(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"WriteEndObject\")) { /* rebalance frames: close member or abort */ }","preventionTips":["Maintain an explicit frame stack in your emission loop","Always end members before ending objects","Count starts vs ends and assert balance at stream end"],"tags":["xaml","invalid-state-transition","nesting"],"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"}