{"record":{"id":"46b192ac6b44d206","repo":"dotnet/wpf","slug":"xamlxmlwriterexception-sr-format-sr-46b192","errorCode":null,"errorMessage":"XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteValue\"))","messagePattern":"XamlXmlWriterException\\(SR\\.Format\\(SR\\.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteValue\"\\)\\)","errorType":"exception","errorClass":"XamlXmlWriterException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs","lineNumber":803,"sourceCode":"\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)\n            {\n                throw new XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteValue\"));\n            }\n\n            public virtual void WriteNamespace(XamlXmlWriter writer, NamespaceDeclaration namespaceDeclaration)\n            {\n                throw new XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteNamespace\"));\n            }\n\n            protected static void WriteMemberAsElement(XamlXmlWriter writer)\n            {\n                Debug.Assert(writer.namespaceScopes.Count > 1);\n\n                Frame frame = writer.namespaceScopes.Peek();\n                Debug.Assert(frame.AllocatingNodeType == XamlNodeType.StartMember);\n\n                XamlType type = frame.Type;\n                XamlMember property = frame.Member;\n\n                string ns;","sourceCodeStart":785,"sourceCodeEnd":821,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs#L785-L821","documentation":"XamlXmlWriter throws XamlXmlWriterException when WriteValue is called in a writer state that cannot accept a value node (the generic WriterState fallback here). Values are only valid in specific contexts, e.g. directly after StartObject or inside a started member; elsewhere the state machine rejects them. This enforces the strict XAML node-stream ordering contract.","triggerScenarios":"Calling XamlXmlWriter.WriteValue without an open member that accepts values, or in states like Start/InRecord where only object/member transitions are allowed.","commonSituations":"Custom XAML writers emitting value before WriteStartMember; writing a value after WriteEndMember; converting code that previously wrote strings into attribute positions incorrectly.","solutions":["Emit WriteStartMember before WriteValue and WriteEndMember after, per the XAML node stream grammar","For object content, ensure the state supports values (after StartObject or inside TryContentProperty-capable states)","Pipe an existing node stream through XamlXmlWriter instead of manual emission"],"exampleFix":"// before\nwriter.WriteStartObject(type);\nwriter.WriteValue(\"text\"); // value outside member\n// after\nwriter.WriteStartObject(type);\nwriter.WriteStartMember(contentMember);\nwriter.WriteValue(\"text\");\nwriter.WriteEndMember();\nwriter.WriteEndObject();","handlingStrategy":"validation","validationCode":"if (!memberOpen || !currentMemberAcceptsValues) throw new InvalidOperationException(\"WriteValue requires an open value-accepting member\");","typeGuard":"bool CanWriteValue(bool memberOpen) => memberOpen;","tryCatchPattern":"try { writer.WriteValue(text); }\ncatch (XamlXmlWriterException ex) { /* recover: close member and re-emit value in valid position */ }","preventionTips":["Only write values inside a started member or directly after StartObject per the XAML node grammar","Use XamlObjectReader->XamlXmlWriter piping for correctness","Unit-test the node emission sequence against the node-stream grammar"],"tags":["xaml","invalid-state-transition","writer"],"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"}