dotnet/wpf · error · XamlXmlWriterException

XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSup…

Error message

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

What it means

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.

Solutions

  1. Emit WriteStartMember before WriteValue and WriteEndMember after, per the XAML node stream grammar
  2. For object content, ensure the state supports values (after StartObject or inside TryContentProperty-capable states)
  3. Pipe an existing node stream through XamlXmlWriter instead of manual emission

Example fix

// before
writer.WriteStartObject(type);
writer.WriteValue("text"); // value outside member
// after
writer.WriteStartObject(type);
writer.WriteStartMember(contentMember);
writer.WriteValue("text");
writer.WriteEndMember();
writer.WriteEndObject();
Defensive patterns

Strategy: validation

Validate before calling

if (!memberOpen || !currentMemberAcceptsValues) throw new InvalidOperationException("WriteValue requires an open value-accepting member");

Type guard

bool CanWriteValue(bool memberOpen) => memberOpen;

Try / catch

try { writer.WriteValue(text); }
catch (XamlXmlWriterException ex) { /* recover: close member and re-emit value in valid position */ }

Prevention

When it happens

Trigger: Calling XamlXmlWriter.WriteValue without an open member that accepts values, or in states like Start/InRecord where only object/member transitions are allowed.

Common situations: Custom XAML writers emitting value before WriteStartMember; writing a value after WriteEndMember; converting code that previously wrote strings into attribute positions incorrectly.

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

Appendix: source

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

            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);

                XamlType type = frame.Type;
                XamlMember property = frame.Member;

                string ns;

View on GitHub (pinned to 81131a70a4)