{"record":{"id":"ce7726b1b9eb7a31","repo":"dotnet/wpf","slug":"xamlxmlwriterexception-sr-format-sr-ce7726","errorCode":null,"errorMessage":"XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteEndMember\"))","messagePattern":"XamlXmlWriterException\\(SR\\.Format\\(SR\\.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteEndMember\"\\)\\)","errorType":"exception","errorClass":"XamlXmlWriterException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs","lineNumber":798,"sourceCode":"        {\n            public virtual void WriteObject(XamlXmlWriter writer, XamlType type, bool isObjectFromMember)\n            {\n                throw new XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, \"WriteObject\"));\n            }\n\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);","sourceCodeStart":780,"sourceCodeEnd":816,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs#L780-L816","documentation":"XamlXmlWriter throws XamlXmlWriterException when a write operation (here WriteEndMember) is invoked while the writer's state machine is in a state that cannot accept that node type. This particular throw site is the fallback WriterState base implementation, meaning the current state has no WriteEndMember handler. It signals a misuse of the XAML node stream ordering contract rather than bad input data.","triggerScenarios":"Calling XamlXmlWriter.WriteEndMember when the writer's currentState does not support member termination — e.g. writing an end-member without a matching WriteStartMember, or writing nodes while the writer is in the Start/Object state.","commonSituations":"Hand-rolling a XAML node loop with XamlXmlWriter/ XamlXmlReader pairs; custom XAML serializers emitting nodes out of order; reusing a writer after it was already closed or in an error state.","solutions":["Fix the node-emission order so every WriteEndMember follows a matching WriteStartMember accepted by the writer state","Drive the writer from a XamlXmlReader/XamlObjectReader node stream instead of hand-emitting nodes so ordering is valid","Inspect the writer state before each call (custom state checks or debug asserts) to detect where the sequence diverges"],"exampleFix":"// before\nwriter.WriteStartObject(type);\nwriter.WriteEndMember(); // no member started\n// after\nwriter.WriteStartObject(type);\nwriter.WriteStartMember(member);\nwriter.WriteValue(value);\nwriter.WriteEndMember();","handlingStrategy":"try-catch","validationCode":"if (writerState != ExpectedMemberOpenState) throw new InvalidOperationException(\"WriteEndMember called without an open member\");","typeGuard":"bool CanWriteEndMember(XamlXmlWriter w, bool memberOpen) => memberOpen;","tryCatchPattern":"try { writer.WriteEndMember(); }\ncatch (XamlXmlWriterException ex) { throw new XamlSerializationException(\"Invalid node order at WriteEndMember\", ex); }","preventionTips":["Mirror reads from a XamlXmlReader/ XamlObjectReader 1:1 instead of hand-emitting nodes","Track a memberOpen flag in your emission loop","Assert start/end pairing in debug builds"],"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"}