dotnet/wpf · error · InvalidOperationException
SR.Format(SR.TextSchema_TextIsNotAllowed…
Error message
SR.Format(SR.TextSchema_TextIsNotAllowed, this.GetType().Name)
What it means
TextElement.AddText throws InvalidOperationException when text content is not permitted under this element's schema and the supplied string is not pure whitespace. Non-whitespace text is rejected with TextSchema_TextIsNotAllowed naming the element type; whitespace-only text is silently ignored.
Solutions
- Wrap the text in a Run inside a schema-valid text container (e.g. Paragraph).
- Remove the AddText call for elements that don't accept text.
- Strip/ignore the text if only whitespace.
Example fix
// before
table.AddText("Hello");
// after
var p = new Paragraph(new Run("Hello"));
flowDocument.Blocks.Add(p); Defensive patterns
Strategy: validation
Validate before calling
bool textAllowed = element is Run || element is Paragraph || element is Span;
if (!textAllowed && text.Trim().Length > 0) throw new InvalidOperationException("Wrap text in a Run inside a Paragraph"); Try / catch
try { element.AddText(text); }
catch (InvalidOperationException) { var p = new Paragraph(new Run(text)); flowDocument.Blocks.Add(p); } Prevention
- Only AddText on text-accepting elements
- Ignore whitespace-only strings for non-text elements
- Wrap raw strings in Run before insertion
When it happens
Trigger: Calling AddText (or XAML text content under an element) where the element's schema disallows text, e.g. AddText on a Table, Section, or UIContainer with non-whitespace string.
Common situations: XAML putting stray text inside container elements; code building documents that appends text to non-text-holding elements.
Understand the failure class
Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.
Related errors
- Can't Assign to Known Member attributes
- SR.Format(SR.InvalidXmlContent…
- SR.Format(SR.InvalidXmlContent…
- SR.Format(SR.RequiredAttributeMissing…
- SR.Format(SR.TextSchema_ChildTypeIsInvalid…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/e5e28ab0847ab6e2.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextElement.cs:883
else
{
// Implicit Run creation
if (TextSchema.IsValidChild(/*parent:*/this, /*childType:*/typeof(Run)))
{
// NOTE: Do not use new Run(text) constructor to avoid TextContainer creation
// which would hit parser perf
Run implicitRun = Inline.CreateImplicitRun(this);
Append(implicitRun);
implicitRun.Text = text;
}
else
{
// Otherwise text is not allowed. Throw if it is not a whitespace
if (text.Trim().Length > 0)
{
throw new InvalidOperationException(SR.Format(SR.TextSchema_TextIsNotAllowed, this.GetType().Name));
}
// As to whitespace - it can be simply ignored
}
}
}
#endregion IAddChild
#region LogicalTree
/// <summary>
/// Returns enumerator to logical children.
/// </summary>
protected internal override IEnumerator LogicalChildren
{
get
{View on GitHub (pinned to 81131a70a4)