dotnet/wpf · error · XamlXmlWriterException

XamlXmlWriterException(SR.XamlXmlWriterWriteObjectNotSupport…

Error message

XamlXmlWriterException(SR.XamlXmlWriterWriteObjectNotSupportedInCurrentState)

What it means

In the Start writer state, when an object arrives from a member context (isObjectFromMember true), XamlXmlWriter must emit it as a property element; but at the root (empty surrounding frame semantics captured in namespaceScopes) there is no member to attach to, so it throws XamlXmlWriterException 'WriteObject not supported in current state'. The root element of a XAML document cannot be produced as an object-from-member.

Solutions

  1. Emit the root with a plain WriteStartObject(XamlType) rather than an object-from-member pattern
  2. Move the member containment one level down so the root is a standalone object element
  3. If transforming a stream, strip the member wrapper before writing the root object

Example fix

// before
writer.WriteGetObject(); // root as object-from-member -> throws
// after
writer.WriteStartObject(rootType);
writer.WriteStartMember(contentMember);
Defensive patterns

Strategy: validation

Validate before calling

if (isRoot && isObjectFromMember) throw new InvalidOperationException("Root object cannot be object-from-member");

Type guard

bool CanWriteRootObject(bool isRoot, bool fromMember) => !isRoot || !fromMember;

Try / catch

try { WriteNode(writer, node); }
catch (XamlXmlWriterException ex) when (ex.Message.Contains("object")) { /* rewrite root as plain StartObject and retry */ }

Prevention

When it happens

Trigger: Writing a node stream whose first object is flagged as coming from a member (GetObject-derived/object-from-member) instead of a plain StartObject, when writing the document root.

Common situations: Custom node-stream builders marking the root object as object-from-member; transformations that wrap content in a member at the top level; hand-built streams replicating markup-extension expansion at root.

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

Appendix: source

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

            {
                get { return state; }
            }

            public override void WriteNamespace(XamlXmlWriter writer, NamespaceDeclaration namespaceDeclaration)
            {
                Debug.Assert(writer.namespaceScopes.Count == 1);
                writer.AssignNamespacePrefix(namespaceDeclaration.Namespace, namespaceDeclaration.Prefix);
            }

            public override void WriteObject(XamlXmlWriter writer, XamlType type, bool isObjectFromMember)
            {
                writer.namespaceScopes.Peek().Type = type;
                writer.namespaceScopes.Peek().IsObjectFromMember = isObjectFromMember;

                if (isObjectFromMember)
                {
                    // The root element cannot be from member
                    throw new XamlXmlWriterException(SR.XamlXmlWriterWriteObjectNotSupportedInCurrentState);
                }
                else
                {
                    WriteStartElementForObject(writer, type);
                    writer.currentState = InRecordTryAttributes.State;
                }
            }
        }

        private class End : WriterState
        {
            private static WriterState state = new End();
            private End() { }

            public static WriterState State
            {
                get { return state; }
            }

View on GitHub (pinned to 81131a70a4)