dotnet/wpf · error · InvalidOperationException

SR.WhiteSpaceInCollection (value, containingXamlType.Name)

Error message

SR.WhiteSpaceInCollection (value, containingXamlType.Name)

What it means

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.

Solutions

  1. Trim the value before calling WriteValue if whitespace is not meaningful.
  2. Add [WhitespaceSignificantCollection] to the target collection type if whitespace must be preserved.
  3. Write a non-whitespace value or skip emitting the whitespace node entirely.
  4. Enable xml:space="preserve" on the enclosing element so the whitespace path is taken.

Example fix

// before
writer.WriteValue("   "); // whitespace into non-significant collection
// after
if (!string.IsNullOrWhiteSpace(value)) writer.WriteValue(value.Trim());
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(value) && !containingType.IsWhitespaceSignificantCollection) value = value?.Trim();

Try / catch

try { writer.WriteValue(value); } catch (InvalidOperationException ex) when (ex.Message.Contains("whitespace")) { writer.WriteValue(value.Trim()); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

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/56d63f5fa63d6bc6. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs:1288

                        // Treat unknown types as WhitespaceSignificantCollections
                        if (containingXamlType is not null && !containingXamlType.IsWhitespaceSignificantCollection)
                        {
                            WriteXmlSpaceOrThrow(writer, value);
                            writer.output.WriteValue(value);
                            writer.currentState = InMemberAfterValue.State;
                        }
                        else if (ContainsConsecutiveInnerSpaces(value) ||
                                 ContainsWhitespaceThatIsNotSpace(value))
                        {
                            if (writer.isFirstElementOfWhitespaceSignificantCollection)
                            {
                                WriteXmlSpaceOrThrow(writer, value);
                                writer.output.WriteValue(value);
                                writer.currentState = InMemberAfterValue.State;
                            }
                            else
                            {
                                throw new InvalidOperationException(SR.Format(SR.WhiteSpaceInCollection, value, containingXamlType.Name));
                            }
                        }
                        else
                        {
                            if (ContainsLeadingSpace(value) && writer.isFirstElementOfWhitespaceSignificantCollection)
                            {
                                WriteXmlSpaceOrThrow(writer, value);
                                writer.output.WriteValue(value);
                                writer.currentState = InMemberAfterValue.State;
                            }

                            if (ContainsTrailingSpace(value))
                            {
                                writer.deferredValue = value;
                                writer.currentState = InMemberAfterValueWithSignificantWhitespace.State;
                            }
                            else
                            {

View on GitHub (pinned to 81131a70a4)