{"record":{"id":"56d63f5fa63d6bc6","repo":"dotnet/wpf","slug":"sr-whitespaceincollection-value-containingxamltype-name","errorCode":null,"errorMessage":"SR.WhiteSpaceInCollection (value, containingXamlType.Name)","messagePattern":"SR\\.WhiteSpaceInCollection \\(value, containingXamlType\\.Name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs","lineNumber":1288,"sourceCode":"                        // Treat unknown types as WhitespaceSignificantCollections\n                        if (containingXamlType is not null && !containingXamlType.IsWhitespaceSignificantCollection)\n                        {\n                            WriteXmlSpaceOrThrow(writer, value);\n                            writer.output.WriteValue(value);\n                            writer.currentState = InMemberAfterValue.State;\n                        }\n                        else if (ContainsConsecutiveInnerSpaces(value) ||\n                                 ContainsWhitespaceThatIsNotSpace(value))\n                        {\n                            if (writer.isFirstElementOfWhitespaceSignificantCollection)\n                            {\n                                WriteXmlSpaceOrThrow(writer, value);\n                                writer.output.WriteValue(value);\n                                writer.currentState = InMemberAfterValue.State;\n                            }\n                            else\n                            {\n                                throw new InvalidOperationException(SR.Format(SR.WhiteSpaceInCollection, value, containingXamlType.Name));\n                            }\n                        }\n                        else\n                        {\n                            if (ContainsLeadingSpace(value) && writer.isFirstElementOfWhitespaceSignificantCollection)\n                            {\n                                WriteXmlSpaceOrThrow(writer, value);\n                                writer.output.WriteValue(value);\n                                writer.currentState = InMemberAfterValue.State;\n                            }\n\n                            if (ContainsTrailingSpace(value))\n                            {\n                                writer.deferredValue = value;\n                                writer.currentState = InMemberAfterValueWithSignificantWhitespace.State;\n                            }\n                            else\n                            {","sourceCodeStart":1270,"sourceCodeEnd":1306,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs#L1270-L1306","documentation":"When writing a string value into a collection, whitespace-only values are rejected if the surrounding type is not a whitespace-significant collection. XAML would otherwise silently lose meaning (a value that is only whitespace cannot be distinguished from formatting), so the writer throws InvalidOperationException with the value and the containing type name.","triggerScenarios":"Calling WriteValue with a string consisting only of whitespace (spaces/newlines) while the current member's containing XamlType is not marked WhitespaceSignificantCollectionAttribute, in the XamlXmlWriter state that handles plain values.","commonSituations":"Re-serializing XAML where indentation between elements was read as node content, writing into custom collections lacking [WhitespaceSignificantCollection], or round-tripping markup-compatibility whitespace into non-UI collections.","solutions":["Trim the value before calling WriteValue if whitespace is not meaningful.","Add [WhitespaceSignificantCollection] to the target collection type if whitespace must be preserved.","Write a non-whitespace value or skip emitting the whitespace node entirely.","Enable xml:space=\"preserve\" on the enclosing element so the whitespace path is taken."],"exampleFix":"// before\nwriter.WriteValue(\"   \"); // whitespace into non-significant collection\n// after\nif (!string.IsNullOrWhiteSpace(value)) writer.WriteValue(value.Trim());","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(value) && !containingType.IsWhitespaceSignificantCollection) value = value?.Trim();","typeGuard":null,"tryCatchPattern":"try { writer.WriteValue(value); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"whitespace\")) { writer.WriteValue(value.Trim()); }","preventionTips":["Trim string values before writing.","Mark collections that need whitespace with [WhitespaceSignificantCollection].","Use xml:space=\"preserve\" intentionally."],"tags":["xaml","whitespace","invalid-argument-value"],"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"}