{"record":{"id":"341c2fca971801b0","repo":"dotnet/wpf","slug":"sr-nonwhitespaceinaddtext","errorCode":null,"errorMessage":"SR.NonWhiteSpaceInAddText","messagePattern":"SR\\.NonWhiteSpaceInAddText","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlSerializerUtil.cs","lineNumber":39,"sourceCode":"    internal static class XamlSerializerUtil\n    {\n        #region Helpers\n\n        /// <summary>\n        ///     Throw an exception if the passed string is not empty and is not\n        ///     all whitespace.  This is used to check IAddChild.AddText calls for\n        ///     object that don't handle text, but may get some whitespace if\n        ///     if xml:space=\"preserve\" is set in xaml.\n        /// </summary>\n        internal static void ThrowIfNonWhiteSpaceInAddText(string s, object parent)\n        {\n            if (s != null)\n            {\n                for (int i = 0; i < s.Length; i++)\n                {\n                   if (!Char.IsWhiteSpace(s[i]))\n                    {\n                        throw new ArgumentException(SR.Format(SR.NonWhiteSpaceInAddText, s));\n                    }\n                }\n            }\n        }\n\n        #endregion Helpers\n    }\n}\n\n","sourceCodeStart":21,"sourceCodeEnd":49,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlSerializerUtil.cs#L21-L49","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Remove the non-whitespace text from the XAML element, or place it inside a property element / content element that supports text.","Wrap the text in a control that accepts text content (e.g. <TextBlock>text</TextBlock>) instead of the container.","Check for invisible non-whitespace characters (e.g. zero-width space) and strip them."],"exampleFix":"// before (XAML)\n<StackPanel>Some stray text</StackPanel>\n// after (XAML)\n<StackPanel>\n  <TextBlock>Some stray text</TextBlock>\n</StackPanel>","handlingStrategy":"validation","validationCode":"static bool IsWhitespaceOnly(string s) => s == null || s.All(char.IsWhiteSpace);","typeGuard":"bool ok = text != null && text.All(char.IsWhiteSpace);","tryCatchPattern":"try { AddText(value); }\ncatch (ArgumentException ex) when (ex.ParamName == null && ex.Message.Contains(\"white space\")) { /* treat as invalid XAML content */ }","preventionTips":["Only pass IXmlTextReader whitespace content to AddText-style APIs.","Trim or reject text nodes in XAML before feeding the parser.","Watch for non-breaking spaces and zero-width characters that look like text vs whitespace."],"tags":["wpf","xaml","parser","validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}