{"record":{"id":"bc115401d63c8bea","repo":"dotnet/wpf","slug":"sr-whitespaceincollection-deferredvalue-containingxamltype","errorCode":null,"errorMessage":"SR.WhiteSpaceInCollection (deferredValue, containingXamlType.Name)","messagePattern":"SR\\.WhiteSpaceInCollection \\(deferredValue, containingXamlType\\.Name\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs","lineNumber":1492,"sourceCode":"                writer.currentState.WriteNamespace(writer, namespaceDeclaration);\n            }\n\n            public override void WriteEndMember(XamlXmlWriter writer)\n            {\n                if (writer.isFirstElementOfWhitespaceSignificantCollection)\n                {\n                    WriteXmlSpace(writer);\n                    writer.output.WriteValue(writer.deferredValue);\n\n                    writer.currentState = InMemberAfterValue.State;\n                    writer.currentState.WriteEndMember(writer);\n\n                    writer.isFirstElementOfWhitespaceSignificantCollection = false;\n                }\n                else\n                {\n                    XamlType containingXamlType = GetContainingXamlType(writer);\n                    throw new InvalidOperationException(SR.Format(SR.WhiteSpaceInCollection, writer.deferredValue, containingXamlType.Name));\n                }\n            }\n\n            public override void WriteObject(XamlXmlWriter writer, XamlType type, bool isObjectFromMember)\n            {\n                writer.output.WriteValue(writer.deferredValue);\n\n                writer.currentState = InMemberAfterValue.State;\n                writer.currentState.WriteObject(writer, type, isObjectFromMember);\n            }\n        }\n\n        // Follows InObject after an End Object.\n        // Like InMember but also allows End Member.\n        //\n        private class InMemberAfterEndObject : WriterState\n        {\n            private static WriterState state = new InMemberAfterEndObject();","sourceCodeStart":1474,"sourceCodeEnd":1510,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs#L1474-L1510","documentation":"In the deferred-value write path, a whitespace-only deferred string is rejected with InvalidOperationException naming the value and the containing XamlType, because the containing type is not a whitespace-significant collection. Same rule as the immediate WriteValue path, but applied to values buffered via writer.deferredValue.","triggerScenarios":"Flush of a deferred value where writer.deferredValue is all whitespace and GetContainingXamlType(writer) yields a XamlType without WhitespaceSignificantCollectionAttribute — e.g. whitespace captured then flushed in a non-collection member context.","commonSituations":"Round-tripping pretty-printed XAML whose indentation was captured as deferred content, writing whitespace between items of ordinary List<T> properties, custom writers deferring values for batching then flushing.","solutions":["Trim deferred values before they are buffered/flushed.","Skip flushing values that are entirely whitespace when the containing type is not whitespace-significant.","Add [WhitespaceSignificantCollection] to the containing collection type.","Set xml:space=\"preserve\" on the enclosing object so the significant-whitespace path is used."],"exampleFix":"// before\nwriter.deferredValue = raw; // may be \"  \"\n// after\nwriter.deferredValue = string.IsNullOrWhiteSpace(raw) ? null : raw.Trim();","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(writer.deferredValue) && !GetContainingXamlType(writer).IsWhitespaceSignificantCollection) skipFlush = true;","typeGuard":null,"tryCatchPattern":"try { FlushDeferred(writer); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"whitespace\")) { writer.deferredValue = null; }","preventionTips":["Normalize deferred values at capture time.","Skip all-whitespace deferred values in non-significant collections.","Mark custom collections with WhitespaceSignificantCollectionAttribute when needed."],"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"}