dotnet/wpf · error · XamlXmlWriterException

XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSup…

Error message

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

What it means

XamlXmlWriter implements a state machine; the base WriterState class defines WriteObject to throw XamlXmlWriterException 'WriteObject not supported in current state'. States that cannot accept an object start (e.g. while inside a member value that already started, or after the stream ended) keep the base implementation, so calling WriteObject in those states fails.

Solutions

  1. Follow the valid node-stream grammar: StartObject → StartMember → value/EndObject → EndMember → EndObject
  2. Re-check your writer loop's state transitions; ensure every StartObject/StartMember has a matching End
  3. Create a fresh XamlXmlWriter if the stream was closed or left in a terminal state

Example fix

// before
writer.WriteStartMember(member);
writer.WriteStartObject(type); // invalid: object directly under member without GetObject semantics
// after
writer.WriteStartMember(member);
writer.WriteGetObject(); // collection/dictionary member expects GetObject, then StartObject
writer.WriteStartObject(itemType);
Defensive patterns

Strategy: try-catch

Validate before calling

// Only call WriteStartObject when no member value or object is in progress (track your own state stack)
if (state != State.ExpectingObject) throw new InvalidOperationException("WriteStartObject not valid here");

Try / catch

try { writer.WriteStartObject(type); }
catch (XamlXmlWriterException) { /* fix node order; restart with fresh writer */ }

Prevention

When it happens

Trigger: Calling XamlXmlWriter.WriteWriteStartObject/WriteGetObject sequence at a point where the current state disallows it — e.g. WriteStartObject after WriteEndDocument, inside an already-started member that expects a value, or twice without closing the first object.

Common situations: Emitting XAML node streams by hand without following the [MS-XAML] node order; adapters converting XML/DOM to a node stream that interleave calls incorrectly; continuing to write after an error or after WriteClose.

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

Appendix: source

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

                {
                    prefixMapList.Add(pair);
                }

                prefixMapList.Sort(CompareByKey);
                return prefixMapList;
            }

            private static int CompareByKey(KeyValuePair<string, string> x, KeyValuePair<string, string> y)
            {
                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)

View on GitHub (pinned to 81131a70a4)