{"record":{"id":"da7b04bd99321d12","repo":"dotnet/wpf","slug":"xamlxmlwriterexception-sr-format-sr-da7b04","errorCode":null,"errorMessage":"XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteObject\"))","messagePattern":"XamlXmlWriterException\\(SR\\.Format\\(SR\\.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteObject\"\\)\\)","errorType":"exception","errorClass":"XamlXmlWriterException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs","lineNumber":783,"sourceCode":"                {\n                    prefixMapList.Add(pair);\n                }\n\n                prefixMapList.Sort(CompareByKey);\n                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)","sourceCodeStart":765,"sourceCodeEnd":801,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs#L765-L801","documentation":"XamlXmlWriter implements a state machine; the base WriterState class defines WriteObject to throw XamlXmlWriterException 'WriteObject not supported in current state'. States that cannot accept an object start (e.g. while inside a member value that already started, or after the stream ended) keep the base implementation, so calling WriteObject in those states fails.","triggerScenarios":"Calling XamlXmlWriter.WriteWriteStartObject/WriteGetObject sequence at a point where the current state disallows it — e.g. WriteStartObject after WriteEndDocument, inside an already-started member that expects a value, or twice without closing the first object.","commonSituations":"Emitting XAML node streams by hand without following the [MS-XAML] node order; adapters converting XML/DOM to a node stream that interleave calls incorrectly; continuing to write after an error or after WriteClose.","solutions":["Follow the valid node-stream grammar: StartObject → StartMember → value/EndObject → EndMember → EndObject","Re-check your writer loop's state transitions; ensure every StartObject/StartMember has a matching End","Create a fresh XamlXmlWriter if the stream was closed or left in a terminal state"],"exampleFix":"// before\nwriter.WriteStartMember(member);\nwriter.WriteStartObject(type); // invalid: object directly under member without GetObject semantics\n// after\nwriter.WriteStartMember(member);\nwriter.WriteGetObject(); // collection/dictionary member expects GetObject, then StartObject\nwriter.WriteStartObject(itemType);","handlingStrategy":"try-catch","validationCode":"// Only call WriteStartObject when no member value or object is in progress (track your own state stack)\nif (state != State.ExpectingObject) throw new InvalidOperationException(\"WriteStartObject not valid here\");","typeGuard":null,"tryCatchPattern":"try { writer.WriteStartObject(type); }\ncatch (XamlXmlWriterException) { /* fix node order; restart with fresh writer */ }","preventionTips":["Mirror the writer's state machine in your emitter","Follow [MS-XAML] node-stream grammar","After any exception, abandon the stream rather than continuing"],"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"}