dotnet/wpf · error · XamlXmlWriterException

XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSup…

Error message

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

What it means

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.

Solutions

  1. Fix the node-emission order so every WriteEndMember follows a matching WriteStartMember accepted by the writer state
  2. Drive the writer from a XamlXmlReader/XamlObjectReader node stream instead of hand-emitting nodes so ordering is valid
  3. Inspect the writer state before each call (custom state checks or debug asserts) to detect where the sequence diverges

Example fix

// before
writer.WriteStartObject(type);
writer.WriteEndMember(); // no member started
// after
writer.WriteStartObject(type);
writer.WriteStartMember(member);
writer.WriteValue(value);
writer.WriteEndMember();
Defensive patterns

Strategy: try-catch

Validate before calling

if (writerState != ExpectedMemberOpenState) throw new InvalidOperationException("WriteEndMember called without an open member");

Type guard

bool CanWriteEndMember(XamlXmlWriter w, bool memberOpen) => memberOpen;

Try / catch

try { writer.WriteEndMember(); }
catch (XamlXmlWriterException ex) { throw new XamlSerializationException("Invalid node order at WriteEndMember", ex); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

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/ce7726b1b9eb7a31. Report an issue: GitHub.

Appendix: source

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

        {
            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)
            {
                Debug.Assert(writer.namespaceScopes.Count > 1);

                Frame frame = writer.namespaceScopes.Peek();
                Debug.Assert(frame.AllocatingNodeType == XamlNodeType.StartMember);

View on GitHub (pinned to 81131a70a4)