dotnet/wpf · error · ArgumentException

SR.NonWhiteSpaceInAddText

Error message

SR.NonWhiteSpaceInAddText

What it means

ThrowIfNonWhiteSpaceInAddText validates the string supplied to an AddText/AddChild text-content path in XAML parsing: only whitespace is allowed there. If the string contains any non-whitespace character, ArgumentException(SR.NonWhiteSpaceInAddText) is thrown with the offending string in the message.

Solutions

  1. Remove the non-whitespace text from the XAML element, or place it inside a property element / content element that supports text.
  2. Wrap the text in a control that accepts text content (e.g. <TextBlock>text</TextBlock>) instead of the container.
  3. Check for invisible non-whitespace characters (e.g. zero-width space) and strip them.

Example fix

// before (XAML)
<StackPanel>Some stray text</StackPanel>
// after (XAML)
<StackPanel>
  <TextBlock>Some stray text</TextBlock>
</StackPanel>
Defensive patterns

Strategy: validation

Validate before calling

static bool IsWhitespaceOnly(string s) => s == null || s.All(char.IsWhiteSpace);

Type guard

bool ok = text != null && text.All(char.IsWhiteSpace);

Try / catch

try { AddText(value); }
catch (ArgumentException ex) when (ex.ParamName == null && ex.Message.Contains("white space")) { /* treat as invalid XAML content */ }

Prevention

When it happens

Trigger: A XAML element routed text content into AddText while the target type only accepts whitespace text; e.g. <Window>hello</Window> or any non-whitespace inner text on a content model that cannot consume it.

Common situations: Typo or stray text inside a XAML element that expects no text content; copy/paste leaving text nodes between elements; whitespace formatting accidentally containing characters such as a non-breaking space is fine (whitespace) but visible text is not.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/341c2fca971801b0. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlSerializerUtil.cs:39

    internal static class XamlSerializerUtil
    {
        #region Helpers

        /// <summary>
        ///     Throw an exception if the passed string is not empty and is not
        ///     all whitespace.  This is used to check IAddChild.AddText calls for
        ///     object that don't handle text, but may get some whitespace if
        ///     if xml:space="preserve" is set in xaml.
        /// </summary>
        internal static void ThrowIfNonWhiteSpaceInAddText(string s, object parent)
        {
            if (s != null)
            {
                for (int i = 0; i < s.Length; i++)
                {
                   if (!Char.IsWhiteSpace(s[i]))
                    {
                        throw new ArgumentException(SR.Format(SR.NonWhiteSpaceInAddText, s));
                    }
                }
            }
        }

        #endregion Helpers
    }
}

View on GitHub (pinned to 81131a70a4)