{"record":{"id":"e28f44db2af94e45","repo":"dotnet/wpf","slug":"xamlxmlwriterexception-sr","errorCode":null,"errorMessage":"XamlXmlWriterException(SR.XamlXmlWriterWriteObjectNotSupportedInCurrentState)","messagePattern":"XamlXmlWriterException\\(SR\\.XamlXmlWriterWriteObjectNotSupportedInCurrentState\\)","errorType":"exception","errorClass":"XamlXmlWriterException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs","lineNumber":966,"sourceCode":"            {\n                get { return state; }\n            }\n\n            public override void WriteNamespace(XamlXmlWriter writer, NamespaceDeclaration namespaceDeclaration)\n            {\n                Debug.Assert(writer.namespaceScopes.Count == 1);\n                writer.AssignNamespacePrefix(namespaceDeclaration.Namespace, namespaceDeclaration.Prefix);\n            }\n\n            public override void WriteObject(XamlXmlWriter writer, XamlType type, bool isObjectFromMember)\n            {\n                writer.namespaceScopes.Peek().Type = type;\n                writer.namespaceScopes.Peek().IsObjectFromMember = isObjectFromMember;\n\n                if (isObjectFromMember)\n                {\n                    // The root element cannot be from member\n                    throw new XamlXmlWriterException(SR.XamlXmlWriterWriteObjectNotSupportedInCurrentState);\n                }\n                else\n                {\n                    WriteStartElementForObject(writer, type);\n                    writer.currentState = InRecordTryAttributes.State;\n                }\n            }\n        }\n\n        private class End : WriterState\n        {\n            private static WriterState state = new End();\n            private End() { }\n\n            public static WriterState State\n            {\n                get { return state; }\n            }","sourceCodeStart":948,"sourceCodeEnd":984,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs#L948-L984","documentation":"In the Start writer state, when an object arrives from a member context (isObjectFromMember true), XamlXmlWriter must emit it as a property element; but at the root (empty surrounding frame semantics captured in namespaceScopes) there is no member to attach to, so it throws XamlXmlWriterException 'WriteObject not supported in current state'. The root element of a XAML document cannot be produced as an object-from-member.","triggerScenarios":"Writing a node stream whose first object is flagged as coming from a member (GetObject-derived/object-from-member) instead of a plain StartObject, when writing the document root.","commonSituations":"Custom node-stream builders marking the root object as object-from-member; transformations that wrap content in a member at the top level; hand-built streams replicating markup-extension expansion at root.","solutions":["Emit the root with a plain WriteStartObject(XamlType) rather than an object-from-member pattern","Move the member containment one level down so the root is a standalone object element","If transforming a stream, strip the member wrapper before writing the root object"],"exampleFix":"// before\nwriter.WriteGetObject(); // root as object-from-member -> throws\n// after\nwriter.WriteStartObject(rootType);\nwriter.WriteStartMember(contentMember);","handlingStrategy":"validation","validationCode":"if (isRoot && isObjectFromMember) throw new InvalidOperationException(\"Root object cannot be object-from-member\");","typeGuard":"bool CanWriteRootObject(bool isRoot, bool fromMember) => !isRoot || !fromMember;","tryCatchPattern":"try { WriteNode(writer, node); }\ncatch (XamlXmlWriterException ex) when (ex.Message.Contains(\"object\")) { /* rewrite root as plain StartObject and retry */ }","preventionTips":["Always emit the document root as a plain WriteStartObject","Keep member containment strictly below the root level","Validate transformed streams before writing"],"tags":["xaml","root-object","invalid-state"],"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"}