{"record":{"id":"00427ba6990982f2","repo":"dotnet/wpf","slug":"sr-xamlxmlwriterwriteobjectnotsupportedincurrentstate","errorCode":null,"errorMessage":"SR.XamlXmlWriterWriteObjectNotSupportedInCurrentState","messagePattern":"SR\\.XamlXmlWriterWriteObjectNotSupportedInCurrentState","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs","lineNumber":1387,"sourceCode":"            public override void WriteObject(XamlXmlWriter writer, XamlType type, bool isObjectFromMember)\n            {\n                Debug.Assert(writer.namespaceScopes.Count > 0);\n                if (writer.namespaceScopes.Peek().AllocatingNodeType != XamlNodeType.StartObject &&\n                    writer.namespaceScopes.Peek().AllocatingNodeType != XamlNodeType.GetObject)\n                {\n                    writer.namespaceScopes.Push(new Frame { AllocatingNodeType = isObjectFromMember ? XamlNodeType.GetObject : XamlNodeType.StartObject });\n                }\n\n                writer.namespaceScopes.Peek().Type = type;\n                writer.namespaceScopes.Peek().IsObjectFromMember = isObjectFromMember;\n\n                writer.isFirstElementOfWhitespaceSignificantCollection = false;\n\n                if (isObjectFromMember)\n                {\n                    if (!writer.namespaceScopes.Peek().IsEmpty())\n                    {\n                        throw new InvalidOperationException(SR.XamlXmlWriterWriteObjectNotSupportedInCurrentState);\n                    }\n\n                    Frame tempFrame = writer.namespaceScopes.Pop();\n                    Frame frame = writer.namespaceScopes.Peek();\n                    writer.namespaceScopes.Push(tempFrame);\n\n                    if (frame.AllocatingNodeType == XamlNodeType.StartMember)\n                    {\n                        XamlType memberType = frame.Member.Type;\n                        if (memberType is not null && !memberType.IsCollection && !memberType.IsDictionary)\n                        {\n                            throw new InvalidOperationException(SR.XamlXmlWriterIsObjectFromMemberSetForArraysOrNonCollections);\n                        }\n                    }\n\n                    writer.currentState = InRecord.State;\n                }\n                else","sourceCodeStart":1369,"sourceCodeEnd":1405,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs#L1369-L1405","documentation":"WriteObject throws InvalidOperationException when an object is being written 'from member' (GetObject semantics) but the current namespace-scope frame is not empty, meaning the writer is not in the plain state expected for that operation. Writing an object via a member reference is only valid when no attribute/namespace data is pending on the current frame.","triggerScenarios":"Calling WriteObject(type, isObjectFromMember: true) while namespaceScopes.Peek() is non-empty — e.g. directives or namespace declarations were recorded on the frame before the GetObject-style object write.","commonSituations":"Custom node streams that emit namespace declarations on a GetObject node, writers forwarding nodes from XamlReader with x:Shared/x:Key attached in the wrong position, incorrect ordering of WriteNamespace before WriteObject(isObjectFromMember).","solutions":["Emit WriteNamespace/attribute nodes only after the object-from-member write completes.","Use WriteStartObject instead of WriteObject(isObjectFromMember:true) when the object has its own namespace declarations.","Check the current frame state with the writer's state machine before calling WriteObject.","Catch InvalidOperationException and log/repair the node stream ordering."],"exampleFix":"// before\nwriter.WriteNamespace(ns); // attaches to current frame\nwriter.WriteObject(type, isObjectFromMember: true); // throws\n// after\nwriter.WriteObject(type, isObjectFromMember: true);\nwriter.WriteNamespace(ns);","handlingStrategy":"validation","validationCode":"if (isObjectFromMember && hasPendingNamespaces) throw new InvalidOperationException(\"WriteNamespace must follow WriteObject(isObjectFromMember:true)\");","typeGuard":null,"tryCatchPattern":"try { writer.WriteObject(type, isObjectFromMember: true); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"WriteObject\")) { /* reorder node stream */ }","preventionTips":["Never emit WriteNamespace while a GetObject-style member object is pending.","Use WriteStartObject when the object carries its own namespaces.","Keep node emission order identical to XamlReader output."],"tags":["xaml","invalid-state-transition","writer-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"}