dotnet/wpf · error · InvalidOperationException
SR.XamlXmlWriterWriteNotSupportedInCurrentState (WriteValue)
Error message
SR.XamlXmlWriterWriteNotSupportedInCurrentState (WriteValue)
What it means
XamlXmlWriter.WriteValue throws InvalidOperationException because WriteValue is only legal while the writer's current frame is positioned at a StartMember (a member is being allocated). The node stream violated the XAML writer state machine, so the requested operation is not valid in the current state.
Solutions
- Ensure StartMember is emitted immediately before WriteValue (correct the node ordering in your writer loop).
- Verify your node stream source (XamlReader) is not dropping or reordering StartMember nodes.
- Wrap the write in try-catch for InvalidOperationException and inspect writer state before continuing.
- If value should belong to the object (e.g. initialization text), call WriteGetObject/StartMember for the content property first.
Example fix
// before
xamlWriter.WriteObject(type);
xamlWriter.WriteValue("text"); // throws: not inside a member
// after
xamlWriter.WriteObject(type);
xamlWriter.StartMember(contentProperty);
xamlWriter.WriteValue("text");
xamlWriter.EndMember(); Defensive patterns
Strategy: try-catch
Validate before calling
if (currentState != XamlNodeType.StartMember) throw new InvalidOperationException("WriteValue requires an active StartMember"); Type guard
bool CanWriteValue(XamlXmlWriter w) => w.State is XamlNodeType.StartMember or XamlNodeType.NamespaceDeclaration;
Try / catch
try { writer.WriteValue(value); } catch (InvalidOperationException ex) when (ex.Message.Contains("WriteValue")) { /* repair node ordering or rethrow with context */ } Prevention
- Emit StartMember before every WriteValue.
- Keep a mirror of the writer state machine in your node loop.
- Test round-trips of complex XAML documents.
When it happens
Trigger: Calling XamlXmlWriter.WriteValue(value) when the top of the namespace-scope stack has AllocatingNodeType != XamlNodeType.StartMember — e.g. writing a value directly after StartObject, between GetObject/StartMember boundaries, or after EndMember without starting a new member.
Common situations: Hand-rolled XAML node loopers that re-emit nodes out of order, custom XamlWriter pipelines that skip StartMember nodes, or code that writes a value for a non-content property expecting it to attach to the object instead of the member.
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
- SR.XamlXmlWriterWriteObjectNotSupportedInCurrentState
- InvalidOperationException(SR.Format(SR.XamlXmlWriterWriteNot…
- SR.TemplateContentSetTwice
- XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSup…
- XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSup…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/f3b3991e7da5c1db.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs:1251
public override void WriteNamespace(XamlXmlWriter writer, NamespaceDeclaration namespaceDeclaration)
{
Debug.Assert(writer.namespaceScopes.Count > 0);
if (writer.namespaceScopes.Peek().AllocatingNodeType != XamlNodeType.StartObject &&
writer.namespaceScopes.Peek().AllocatingNodeType != XamlNodeType.GetObject)
{
writer.namespaceScopes.Push(new Frame { AllocatingNodeType = XamlNodeType.StartObject });
}
writer.AssignNamespacePrefix(namespaceDeclaration.Namespace, namespaceDeclaration.Prefix);
}
public override void WriteValue(XamlXmlWriter writer, string value)
{
Frame frame = writer.namespaceScopes.Peek();
if (frame.AllocatingNodeType != XamlNodeType.StartMember)
{
throw new InvalidOperationException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, "WriteValue"));
}
if (frame.Member.DeclaringType == XamlLanguage.XData)
{
writer.output.WriteRaw(value);
writer.currentState = InMemberAfterValue.State;
}
else
{
// If we have significant white space, we write out xml:space='preserve'
// except if it is a WhiteSpace Significant collection. In that case
// we write that out only if
// 1. It has 2 consecutive spaces or non space whitespace (tabs, new line, etc)
// 2. First element has leading whitespace
// 3. Last Element has trailing whitespace
if (HasSignificantWhitespace(value))
{
XamlType containingXamlType = GetContainingXamlType(writer);View on GitHub (pinned to 81131a70a4)