dotnet/wpf · error · XamlXmlWriterException
XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSup…
Error message
XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, "WriteEndObject"))
What it means
The base WriterState defines WriteEndObject to throw XamlXmlWriterException 'WriteEndObject not supported in current state'. Only states representing an open object element support WriteEndObject; calling it from any other state (no open object, inside a member, after close) throws.
Solutions
- Balance every WriteStartObject/WriteGetObject with exactly one WriteEndObject in LIFO order
- Track nesting depth in your emitter and assert balance before WriteClose
- Fix exception/recovery paths so partial writes are abandoned (new writer) rather than resumed
Example fix
// before writer.WriteStartObject(type); WriteStartMember(m); WriteEndObject(); // member still open // after writer.WriteStartObject(type); WriteStartMember(m); WriteEndMember(); WriteEndObject();
Defensive patterns
Strategy: validation
Validate before calling
if (openObjectDepth <= 0) throw new InvalidOperationException("WriteEndObject without open object"); Try / catch
try { writer.WriteEndObject(); }
catch (XamlXmlWriterException) { /* unbalanced end; restart with fresh writer */ } Prevention
- Keep a nesting-depth counter for Start/End calls
- Close members (EndMember) before ending their object
- Assert balanced counts before WriteClose
When it happens
Trigger: Calling WriteEndObject when there is no matching open object — unbalanced End calls, calling WriteEndObject after WriteEndMember closed the value, or calling it twice for one StartObject.
Common situations: Hand-rolled node-stream emitters with mismatched Start/End counters; error-recovery paths that skip EndMember/EndObject calls then continue writing; adapters from other serialization formats mapping element close events naively.
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
- XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSup…
- XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSup…
- IAmbientProvider
- InvalidOperationException(SR.Format(SR.XamlXmlWriterWriteNot…
- InvalidOperationException(SR.Format(SR.XamlXmlWriterWriteNot…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/51b681a70217db34.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs:788
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)
{
throw new XamlXmlWriterException(SR.Format(SR.XamlXmlWriterWriteNotSupportedInCurrentState, "WriteValue"));
}
public virtual void WriteNamespace(XamlXmlWriter writer, NamespaceDeclaration namespaceDeclaration)View on GitHub (pinned to 81131a70a4)