{"record":{"id":"f3b3991e7da5c1db","repo":"dotnet/wpf","slug":"sr-xamlxmlwriterwritenotsupportedincurrentstate-writevalue","errorCode":null,"errorMessage":"SR.XamlXmlWriterWriteNotSupportedInCurrentState (WriteValue)","messagePattern":"SR\\.XamlXmlWriterWriteNotSupportedInCurrentState \\(WriteValue\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs","lineNumber":1251,"sourceCode":"\n            public override void WriteNamespace(XamlXmlWriter writer, NamespaceDeclaration namespaceDeclaration)\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 = XamlNodeType.StartObject });\n                }\n\n                writer.AssignNamespacePrefix(namespaceDeclaration.Namespace, namespaceDeclaration.Prefix);\n            }\n\n            public override void WriteValue(XamlXmlWriter writer, string value)\n            {\n                Frame frame = writer.namespaceScopes.Peek();\n                if (frame.AllocatingNodeType != XamlNodeType.StartMember)\n                {\n                    throw new InvalidOperationException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteValue\"));\n                }\n\n                if (frame.Member.DeclaringType == XamlLanguage.XData)\n                {\n                    writer.output.WriteRaw(value);\n                    writer.currentState = InMemberAfterValue.State;\n                }\n                else\n                {\n                    // If we have significant white space, we write out xml:space='preserve'\n                    // except if it is a WhiteSpace Significant collection. In that case\n                    // we write that out only if\n                    // 1. It has 2 consecutive spaces or non space whitespace (tabs, new line, etc)\n                    // 2. First element has leading whitespace\n                    // 3. Last Element has trailing whitespace\n                    if (HasSignificantWhitespace(value))\n                    {\n                        XamlType containingXamlType = GetContainingXamlType(writer);","sourceCodeStart":1233,"sourceCodeEnd":1269,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs#L1233-L1269","documentation":"XamlXmlWriter.WriteValue throws InvalidOperationException because WriteValue is only legal while the writer's current frame is positioned at a StartMember (a member is being allocated). The node stream violated the XAML writer state machine, so the requested operation is not valid in the current state.","triggerScenarios":"Calling XamlXmlWriter.WriteValue(value) when the top of the namespace-scope stack has AllocatingNodeType != XamlNodeType.StartMember — e.g. writing a value directly after StartObject, between GetObject/StartMember boundaries, or after EndMember without starting a new member.","commonSituations":"Hand-rolled XAML node loopers that re-emit nodes out of order, custom XamlWriter pipelines that skip StartMember nodes, or code that writes a value for a non-content property expecting it to attach to the object instead of the member.","solutions":["Ensure StartMember is emitted immediately before WriteValue (correct the node ordering in your writer loop).","Verify your node stream source (XamlReader) is not dropping or reordering StartMember nodes.","Wrap the write in try-catch for InvalidOperationException and inspect writer state before continuing.","If value should belong to the object (e.g. initialization text), call WriteGetObject/StartMember for the content property first."],"exampleFix":"// before\nxamlWriter.WriteObject(type);\nxamlWriter.WriteValue(\"text\"); // throws: not inside a member\n// after\nxamlWriter.WriteObject(type);\nxamlWriter.StartMember(contentProperty);\nxamlWriter.WriteValue(\"text\");\nxamlWriter.EndMember();","handlingStrategy":"try-catch","validationCode":"if (currentState != XamlNodeType.StartMember) throw new InvalidOperationException(\"WriteValue requires an active StartMember\");","typeGuard":"bool CanWriteValue(XamlXmlWriter w) => w.State is XamlNodeType.StartMember or XamlNodeType.NamespaceDeclaration;","tryCatchPattern":"try { writer.WriteValue(value); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"WriteValue\")) { /* repair node ordering or rethrow with context */ }","preventionTips":["Emit StartMember before every WriteValue.","Keep a mirror of the writer state machine in your node loop.","Test round-trips of complex XAML documents."],"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"}