dotnet/wpf · error · InvalidOperationException
SR.XamlXmlWriterWriteNotSupportedInCurrentState (operation)
Error message
SR.XamlXmlWriterWriteNotSupportedInCurrentState (operation)
What it means
XamlXmlWriter's ThrowIfFailed helper throws InvalidOperationException stating that the given write operation is not supported in the writer's current state. XamlXmlWriter is a state machine over the XAML node stream; calling an operation that is invalid at the current node (e.g. writing an object where a member/value is expected) aborts the write.
Solutions
- Emit XAML node-stream operations in valid order (StartObject → members → values → EndObject).
- Trace the node stream (e.g. with XamlXmlReader loop) to see which node precedes the failing call.
- Only call writer APIs driven by a matching XamlReader.Read() loop rather than ad-hoc calls.
Example fix
// before: writing a member without an open object writer.WriteStartMember(xamlMember); // after: open the object first writer.WriteStartObject(xamlType); writer.WriteStartMember(xamlMember);
Defensive patterns
Strategy: try-catch
Validate before calling
// Track the last node written; only call operations valid for it bool canWriteObject = lastNode is null or XamlNodeType.StartMember or XamlNodeType.NamespaceDeclaration;
Try / catch
try { writer.WriteObject(w, type, isFromMember); }
catch (InvalidOperationException ex) when (ex.Message.Contains("not supported")) { /* state bug; dump node stream */ } Prevention
- Drive the writer strictly from a XamlReader loop (reader.Read() → writer.Write(node)).
- Never interleave ad-hoc write calls with reader-driven ones.
- Log XAML node stream positions while debugging custom writers.
When it happens
Trigger: Calling any IXamlXmlWriter write method (operation name is passed as the `operation` argument) while the writer's state does not permit it, e.g. WriteEndMember/WriteValue out of order, writing an object while inside a member that cannot contain it.
Common situations: Hand-written or custom XAML node stream producers emitting nodes out of order; piping an XamlReader stream from mismatched input into the writer; mixing WriteGetObject and WriteObject 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
- IAmbientProvider
- InvalidOperationException(SR.Format(SR.XamlXmlWriterWriteNot…
- InvalidOperationException(SR.Format(SR.XamlXmlWriterWriteNot…
- InvalidOperationException(SR.XamlTypeNameNameIsNullOrEmpty)
- InvalidOperationException(SR.XamlTypeNameNamespaceIsNull)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/39136f32b5b21cb9.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs:2040
{
var ppNodesList = writer.ppStateInfo.NodesList;
writer.ppStateInfo.Reset();
writer.currentState = writer.ppStateInfo.ReturnState;
foreach (List<XamlNode> nodesList in ppNodesList)
{
foreach (XamlNode node in nodesList)
{
writer.currentState.WriteNode(writer, node);
}
}
}
private void ThrowIfFailed(bool fail, string operation)
{
if (fail)
{
throw new InvalidOperationException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, operation));
}
}
public override void WriteObject(XamlXmlWriter writer, XamlType type, bool isObjectFromMember)
{
if (!isObjectFromMember)
{
writer.ppStateInfo.Writer.WriteStartObject(type);
ThrowIfFailed(writer.ppStateInfo.Writer.Failed, "WriteStartObject");
XamlNode node = new XamlNode(XamlNodeType.StartObject, type);
if (writer.ppStateInfo.CurrentDepth == 0)
{
writer.ppStateInfo.NodesList.Add(new List<XamlNode> { node });
}
else
{
writer.ppStateInfo.NodesList[writer.ppStateInfo.NodesList.Count - 1].Add(node);View on GitHub (pinned to 81131a70a4)