dotnet/wpf · error
SR.Format(SR.MarkupWriter_CannotSerializeNestedPublictype…
Error message
SR.Format(SR.MarkupWriter_CannotSerializeNestedPublictype, type.ToString())
What it means
MarkupWriter.VerifyTypeIsSerializable throws this InvalidOperationException when asked to serialize a type that is a nested public type (type.IsNestedPublic). XAML serialization in WPF only supports top-level public types, because a nested type cannot be referenced unambiguously in generated XAML. This guard runs at the start of WriteItem for every type being written.
Solutions
- Move the nested public type to the namespace level (declare it outside the containing class) so it becomes a top-level public type.
- If the nested type must stay nested, serialize via a surrogate top-level wrapper type instead.
- Replace XamlWriter.Save with a custom serializer (DataContractSerializer, JSON) for types the XAML writer cannot handle.
Example fix
// before
public class Window1 : Window {
public class MyData { public string Name { get; set; } }
void Save(object o) { XamlWriter.Save(o); } // InvalidOperationException: CannotSerializeNestedPublictype
}
// after
public class MyData { public string Name { get; set; } }
public class Window1 : Window {
void Save(object o) { XamlWriter.Save(o); }
} Defensive patterns
Strategy: validation
Validate before calling
static bool IsXamlSerializable(Type t) =>
t != null && !t.IsNested && t.IsPublic && !t.IsGenericType;
// before saving: if (!IsXamlSerializable(obj.GetType())) ... Type guard
static bool IsTopLevelPublic(Type t) => !t.IsNested && t.IsPublic;
Try / catch
try { XamlWriter.Save(obj, stream); }
catch (InvalidOperationException ex) when (ex.Message.Contains("nested"))
{
// fall back to another serializer or move the type
} Prevention
- Never declare data types nested inside classes if they will be XAML-serialized.
- Keep serializable DTOs as top-level public types in their own files.
- Add a unit test that runs VerifyTypeIsSerializable-equivalent checks on all serializable model types.
When it happens
Trigger: Calling MarkupWriter.WriteItem (directly or via XamlWriter.Save) on an object whose runtime type is a public class declared inside another class.
Common situations: Developers model data objects as classes nested inside a Window, App, or ViewModel class and then call XamlWriter.Save on them; sample code often nests small data classes for convenience.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- SR.Format(SR.MarkupWriter_CannotSerializeNonPublictype…
- ArgumentNullException(nameof(writer))
- (no message - parameterless InvalidOperationException)
- SR.Format(SR.DeferringLoaderNoSave…
- SR.Format(SR.MarkupWriter_CannotSerializeGenerictype…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/a53f48212d473144.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Primitives/MarkupWriter.cs:143
writer.Flush();
}
}
/// <summary>
/// Throws an exception if the given type cannot be serialized because it is
/// 1) not public
/// 2) a nested-public
/// 3) a generic type
/// </summary>
/// <param name="type">
/// The type to be checked
/// </param>
internal static void VerifyTypeIsSerializable(Type type)
{
// Check the type to make sure that it is not a nested type, that it is public, and that it is not generic
if (type.IsNestedPublic)
{
throw new InvalidOperationException( SR.Format( SR.MarkupWriter_CannotSerializeNestedPublictype, type.ToString() ));
}
if (!type.IsPublic )
{
throw new InvalidOperationException( SR.Format( SR.MarkupWriter_CannotSerializeNonPublictype, type.ToString() ));
}
if (type.IsGenericType)
{
throw new InvalidOperationException( SR.Format( SR.MarkupWriter_CannotSerializeGenerictype, type.ToString() ));
}
}
#region Internal Implementation
internal MarkupWriter(XmlWriter writer)
{
_writer = writer;
_xmlTextWriter = writer as XmlTextWriter;
}
View on GitHub (pinned to 81131a70a4)