{"record":{"id":"39136f32b5b21cb9","repo":"dotnet/wpf","slug":"sr-xamlxmlwriterwritenotsupportedincurrentstate-operation","errorCode":null,"errorMessage":"SR.XamlXmlWriterWriteNotSupportedInCurrentState (operation)","messagePattern":"SR\\.XamlXmlWriterWriteNotSupportedInCurrentState \\(operation\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs","lineNumber":2040,"sourceCode":"            {\n                var ppNodesList = writer.ppStateInfo.NodesList;\n                writer.ppStateInfo.Reset();\n                writer.currentState = writer.ppStateInfo.ReturnState;\n\n                foreach (List<XamlNode> nodesList in ppNodesList)\n                {\n                    foreach (XamlNode node in nodesList)\n                    {\n                        writer.currentState.WriteNode(writer, node);\n                    }\n                }\n            }\n\n            private void ThrowIfFailed(bool fail, string operation)\n            {\n                if (fail)\n                {\n                    throw new InvalidOperationException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, operation));\n                }\n            }\n\n            public override void WriteObject(XamlXmlWriter writer, XamlType type, bool isObjectFromMember)\n            {\n                if (!isObjectFromMember)\n                {\n                    writer.ppStateInfo.Writer.WriteStartObject(type);\n                    ThrowIfFailed(writer.ppStateInfo.Writer.Failed, \"WriteStartObject\");\n\n                    XamlNode node = new XamlNode(XamlNodeType.StartObject, type);\n                    if (writer.ppStateInfo.CurrentDepth == 0)\n                    {\n                        writer.ppStateInfo.NodesList.Add(new List<XamlNode> { node });\n                    }\n                    else\n                    {\n                        writer.ppStateInfo.NodesList[writer.ppStateInfo.NodesList.Count - 1].Add(node);","sourceCodeStart":2022,"sourceCodeEnd":2058,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs#L2022-L2058","documentation":"XamlXmlWriter's ThrowIfFailed helper throws InvalidOperationException stating that the given write operation is not supported in the writer's current state. XamlXmlWriter is a state machine over the XAML node stream; calling an operation that is invalid at the current node (e.g. writing an object where a member/value is expected) aborts the write.","triggerScenarios":"Calling any IXamlXmlWriter write method (operation name is passed as the `operation` argument) while the writer's state does not permit it, e.g. WriteEndMember/WriteValue out of order, writing an object while inside a member that cannot contain it.","commonSituations":"Hand-written or custom XAML node stream producers emitting nodes out of order; piping an XamlReader stream from mismatched input into the writer; mixing WriteGetObject and WriteObject incorrectly.","solutions":["Emit XAML node-stream operations in valid order (StartObject → members → values → EndObject).","Trace the node stream (e.g. with XamlXmlReader loop) to see which node precedes the failing call.","Only call writer APIs driven by a matching XamlReader.Read() loop rather than ad-hoc calls."],"exampleFix":"// before: writing a member without an open object\nwriter.WriteStartMember(xamlMember);\n// after: open the object first\nwriter.WriteStartObject(xamlType);\nwriter.WriteStartMember(xamlMember);","handlingStrategy":"try-catch","validationCode":"// Track the last node written; only call operations valid for it\nbool canWriteObject = lastNode is null or XamlNodeType.StartMember or XamlNodeType.NamespaceDeclaration;","typeGuard":null,"tryCatchPattern":"try { writer.WriteObject(w, type, isFromMember); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"not supported\")) { /* state bug; dump node stream */ }","preventionTips":["Drive the writer strictly from a XamlReader loop (reader.Read() → writer.Write(node)).","Never interleave ad-hoc write calls with reader-driven ones.","Log XAML node stream positions while debugging custom writers."],"tags":["xaml","invalid-state","state-machine"],"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"}