{"record":{"id":"488a136536883551","repo":"dotnet/wpf","slug":"sr-format-sr-nonwhitespaceinaddtext-s","errorCode":null,"errorMessage":"SR.Format(SR.NonWhiteSpaceInAddText, s)","messagePattern":"SR\\.Format\\(SR\\.NonWhiteSpaceInAddText, s\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Markup/XamlSerializerUtil.cs","lineNumber":56,"sourceCode":"                throw new InvalidOperationException();\n            }\n        }\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        ///     objects 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)\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}\n\n","sourceCodeStart":38,"sourceCodeEnd":64,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Markup/XamlSerializerUtil.cs#L38-L64","documentation":"XamlSerializerUtil.ThrowIfNonWhiteSpaceInAddText throws ArgumentException when a string containing non-whitespace characters is passed to AddText on an element that cannot accept text content. WPF calls it from AddText implementations of content-less controls to reject stray text inside their XAML tags.","triggerScenarios":"Putting text (or a typo'd character) directly inside the element tags of a control whose AddText forbids content, e.g. <Grid>abc</Grid> or a non-breaking space/hidden character in a container's XAML.","commonSituations":"Stray whitespace-looking characters like &nbsp; (U+00A0), BOM or zero-width characters pasted into XAML between container tags; leftover text after refactoring a control from a TextBlock to a content-less panel; invalid children placed as raw text.","solutions":["Remove the text between the element's open/close tags, or wrap it in a valid content element (e.g. <TextBlock Text=\"...\"/>)","Check for invisible non-whitespace characters (U+00A0, zero-width space, BOM) — retype the whitespace or re-save the XAML as clean UTF-8","If the control should accept text, switch to a control type that supports text content (TextBlock, ContentControl with string content)"],"exampleFix":"// before\n<Grid>\n  Leftover text\n</Grid>\n// after\n<Grid>\n  <TextBlock Text=\"Leftover text\"/>\n</Grid>","handlingStrategy":"validation","validationCode":"bool isWhitespaceOnly = string.IsNullOrEmpty(s) || s.All(char.IsWhiteSpace);","typeGuard":"static bool IsWhiteSpaceOnly(string s) => string.IsNullOrEmpty(s) || s.All(char.IsWhiteSpace);","tryCatchPattern":"try { serializerUtil.ThrowIfNonWhiteSpaceInAddText(text); }\ncatch (ArgumentException ex) { /* strip or route non-whitespace text */ }","preventionTips":["Never put raw text inside content-less container tags in XAML","Watch for U+00A0 and zero-width characters that look like whitespace","Wrap desired text in a TextBlock or ContentControl"],"tags":["wpf","xaml","argument","parsing"],"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"}