dotnet/wpf · error · XamlXmlWriterException

XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSup…

Error message

XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, "WriteStartMember"))

What it means

The base WriterState defines WriteStartMember to throw XamlXmlWriterException 'WriteStartMember not supported in current state'. StartMember is only valid while an object is open and no member/value write is in progress; other states keep the throwing base implementation.

Solutions

  1. Only call WriteStartMember between WriteStartObject/WriteGetObject and the matching WriteEndObject
  2. Always call WriteEndMember before starting the next member
  3. Validate node-stream ordering in your emitter (e.g. maintain a state stack mirroring the writer's)

Example fix

// before
writer.WriteStartObject(type);
writer.WriteStartMember(m1); WriteValue(v1);
writer.WriteStartMember(m2); // m1 not ended
// after
writer.WriteStartObject(type);
writer.WriteStartMember(m1); WriteValue(v1); writer.WriteEndMember();
writer.WriteStartMember(m2); WriteValue(v2); writer.WriteEndMember();
Defensive patterns

Strategy: try-catch

Validate before calling

if (state != State.InObject || state == State.InMember) throw new InvalidOperationException("WriteStartMember not valid here");

Try / catch

try { writer.WriteStartMember(member); }
catch (XamlXmlWriterException) { /* wrong state; fix emitter ordering */ }

Prevention

When it happens

Trigger: Calling WriteStartMember when no object is open (before StartObject or after EndObject), inside another member that is still open, or after the document/stream has been closed.

Common situations: Emitting property writes outside an object context; forgetting to EndMember before starting the next member; writing members after EndObject due to a loop-boundary bug in the emitter.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/55dcffc081c654f9. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs:793

                return string.Compare(x.Key, y.Key, false, TypeConverterHelper.InvariantEnglishUS);
            }
        }

        private abstract class WriterState
        {
            public virtual void WriteObject(XamlXmlWriter writer, XamlType type, bool isObjectFromMember)
            {
                throw new XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, "WriteObject"));
            }

            public virtual void WriteEndObject(XamlXmlWriter writer)
            {
                throw new XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, "WriteEndObject"));
            }

            public virtual void WriteStartMember(XamlXmlWriter writer, XamlMember property)
            {
                throw new XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, "WriteStartMember"));
            }

            public virtual void WriteEndMember(XamlXmlWriter writer)
            {
                throw new XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, "WriteEndMember"));
            }

            public virtual void WriteValue(XamlXmlWriter writer, string value)
            {
                throw new XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, "WriteValue"));
            }

            public virtual void WriteNamespace(XamlXmlWriter writer, NamespaceDeclaration namespaceDeclaration)
            {
                throw new XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, "WriteNamespace"));
            }

            protected static void WriteMemberAsElement(XamlXmlWriter writer)

View on GitHub (pinned to 81131a70a4)