dotnet/wpf · error · XamlXmlWriterException

XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSup…

Error message

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

What it means

XamlXmlWriter throws XamlXmlWriterException when WriteNamespace is called from a state whose WriterState does not implement namespace writing (the base fallback shown). Namespace declarations can only be written while an object element is being opened, so calling it elsewhere violates the writer's state machine. The error text names the disallowed call to pinpoint the contract break.

Solutions

  1. Call WriteNamespace immediately after WriteStartObject/WriteGetObject and before members, as the state machine expects
  2. Ensure required namespaces are part of the schema context so they are emitted automatically
  3. Restructure the emission loop to follow the XAML node stream ordering

Example fix

// before
writer.WriteNamespace(ns);
writer.WriteStartObject(type);
// after
writer.WriteStartObject(type);
writer.WriteNamespace(ns);
writer.WriteStartMember(member);
Defensive patterns

Strategy: validation

Validate before calling

if (!justWroteStartObject) throw new InvalidOperationException("WriteNamespace must follow WriteStartObject/WriteGetObject");

Type guard

bool CanWriteNamespace(bool inStartElementOpenPhase) => inStartElementOpenPhase;

Try / catch

try { writer.WriteNamespace(nsDecl); }
catch (XamlXmlWriterException ex) { /* buffer the namespace and emit after next StartObject */ }

Prevention

When it happens

Trigger: Calling XamlXmlWriter.WriteNamespace(NamespaceDeclaration) after the start element has already been written or before any object was started, from a state without a WriteNamespace override.

Common situations: Emitting all namespace declarations up-front before WriteStartObject; adding namespaces mid-document in custom XAML generation; porting code from XmlWriter semantics to XamlXmlWriter.

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

Appendix: source

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

            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;

                XamlType xamlType = property.IsAttachable ? property.DeclaringType : type;
                string prefix = property.IsAttachable || property.IsDirective ? writer.FindPrefix(property.GetXamlNamespaces(), out ns) : writer.FindPrefix(type.GetXamlNamespaces(), out ns);
                string local = (property.IsDirective) ? property.Name : $"{GetTypeName(xamlType)}.{property.Name}";
                writer.output.WriteStartElement(prefix, local, ns);

View on GitHub (pinned to 81131a70a4)