{"record":{"id":"51b681a70217db34","repo":"dotnet/wpf","slug":"xamlxmlwriterexception-sr-format-sr-51b681","errorCode":null,"errorMessage":"XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteEndObject\"))","messagePattern":"XamlXmlWriterException\\(SR\\.Format\\(SR\\.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteEndObject\"\\)\\)","errorType":"exception","errorClass":"XamlXmlWriterException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs","lineNumber":788,"sourceCode":"                return prefixMapList;\n            }\n\n            private static int CompareByKey(KeyValuePair<string, string> x, KeyValuePair<string, string> y)\n            {\n                return string.Compare(x.Key, y.Key, false, TypeConverterHelper.InvariantEnglishUS);\n            }\n        }\n\n        private abstract class WriterState\n        {\n            public virtual void WriteObject(XamlXmlWriter writer, XamlType type, bool isObjectFromMember)\n            {\n                throw new XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteObject\"));\n            }\n\n            public virtual void WriteEndObject(XamlXmlWriter writer)\n            {\n                throw new XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteEndObject\"));\n            }\n\n            public virtual void WriteStartMember(XamlXmlWriter writer, XamlMember property)\n            {\n                throw new XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteStartMember\"));\n            }\n\n            public virtual void WriteEndMember(XamlXmlWriter writer)\n            {\n                throw new XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteEndMember\"));\n            }\n\n            public virtual void WriteValue(XamlXmlWriter writer, string value)\n            {\n                throw new XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteValue\"));\n            }\n\n            public virtual void WriteNamespace(XamlXmlWriter writer, NamespaceDeclaration namespaceDeclaration)","sourceCodeStart":770,"sourceCodeEnd":806,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs#L770-L806","documentation":"The base WriterState defines WriteEndObject to throw XamlXmlWriterException 'WriteEndObject not supported in current state'. Only states representing an open object element support WriteEndObject; calling it from any other state (no open object, inside a member, after close) throws.","triggerScenarios":"Calling WriteEndObject when there is no matching open object — unbalanced End calls, calling WriteEndObject after WriteEndMember closed the value, or calling it twice for one StartObject.","commonSituations":"Hand-rolled node-stream emitters with mismatched Start/End counters; error-recovery paths that skip EndMember/EndObject calls then continue writing; adapters from other serialization formats mapping element close events naively.","solutions":["Balance every WriteStartObject/WriteGetObject with exactly one WriteEndObject in LIFO order","Track nesting depth in your emitter and assert balance before WriteClose","Fix exception/recovery paths so partial writes are abandoned (new writer) rather than resumed"],"exampleFix":"// before\nwriter.WriteStartObject(type); WriteStartMember(m); WriteEndObject(); // member still open\n// after\nwriter.WriteStartObject(type); WriteStartMember(m); WriteEndMember(); WriteEndObject();","handlingStrategy":"validation","validationCode":"if (openObjectDepth <= 0) throw new InvalidOperationException(\"WriteEndObject without open object\");","typeGuard":null,"tryCatchPattern":"try { writer.WriteEndObject(); }\ncatch (XamlXmlWriterException) { /* unbalanced end; restart with fresh writer */ }","preventionTips":["Keep a nesting-depth counter for Start/End calls","Close members (EndMember) before ending their object","Assert balanced counts before WriteClose"],"tags":["xaml","invalid-state","node-stream"],"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"}