{"record":{"id":"199e932ccfc23188","repo":"dotnet/wpf","slug":"sr-cannotwritexmlspacepreserveonmember-member-value","errorCode":null,"errorMessage":"SR.CannotWriteXmlSpacePreserveOnMember (member, value)","messagePattern":"SR\\.CannotWriteXmlSpacePreserveOnMember \\(member, value\\)","errorType":"exception","errorClass":"XamlXmlWriterException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs","lineNumber":1330,"sourceCode":"                    else\n                    {\n                        writer.output.WriteValue(value);\n                        writer.currentState = InMemberAfterValue.State;\n                    }\n                }\n\n                if (writer.currentState != InMemberAfterValueWithSignificantWhitespace.State)\n                {\n                    writer.isFirstElementOfWhitespaceSignificantCollection = false;\n                }\n            }\n\n            private void WriteXmlSpaceOrThrow(XamlXmlWriter writer, string value)\n            {\n                var frameWithXmlSpacePreserve = FindFrameWithXmlSpacePreserve(writer);\n                if (frameWithXmlSpacePreserve.AllocatingNodeType == XamlNodeType.StartMember)\n                {\n                    throw new XamlXmlWriterException(SR.Format(SR.CannotWriteXmlSpacePreserveOnMember, frameWithXmlSpacePreserve.Member, value));\n                }\n\n                WriteXmlSpace(writer);\n            }\n\n            // this method finds the SO or SM where \"xml:space = preserve\" will actually be attached to\n            private Frame FindFrameWithXmlSpacePreserve(XamlXmlWriter writer)\n            {\n                var frameEnumerator = writer.namespaceScopes.GetEnumerator();\n\n                while (frameEnumerator.MoveNext())\n                {\n                    var frame = frameEnumerator.Current;\n                    if (frame.AllocatingNodeType == XamlNodeType.GetObject)\n                    {\n                        continue;\n                    }\n","sourceCodeStart":1312,"sourceCodeEnd":1348,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlXmlWriter.cs#L1312-L1348","documentation":"WriteXmlSpaceOrThrow throws XamlXmlWriterException when the frame where xml:space=\"preserve\" must be attached is a StartMember. The XAML spec forbids attaching xml:space to a member (attribute) position in the output being produced, so writing the value that required preserve is refused.","triggerScenarios":"Writing a whitespace-containing value where xml:space preserve is in effect and FindFrameWithXmlSpacePreserve resolves to a frame whose AllocatingNodeType is StartMember — i.e. the preserve directive would have to be emitted on a member rather than an object/start-tag.","commonSituations":"Round-tripping XAML with xml:space=\"preserve\" placed on a property element instead of the object element, custom serializers preserving indented text inside members, migrations from XamlXmlWriter 3.5 behavior.","solutions":["Move xml:space=\"preserve\" from the property/member onto the containing object element.","Remove xml:space=\"preserve\" and normalize the whitespace in the value.","Catch XamlXmlWriterException and fall back to writing the value without preserve.","Restructure the node stream so the preserved value is written under an object frame."],"exampleFix":"<!-- before -->\n<TextBlock><TextBlock.Text xml:space=\"preserve\">  hi  </TextBlock.Text></TextBlock>\n<!-- after -->\n<TextBlock xml:space=\"preserve\"><TextBlock.Text>  hi  </TextBlock.Text></TextBlock>","handlingStrategy":"try-catch","validationCode":"// before writing a preserved value, confirm xml:space is on an object element, not a member element","typeGuard":null,"tryCatchPattern":"try { writer.WriteValue(preserved); } catch (XamlXmlWriterException ex) when (ex.Message.Contains(\"xml:space\")) { /* drop preserve or move directive */ }","preventionTips":["Place xml:space=\"preserve\" on object elements only.","Avoid preserve directives on property elements when re-serializing.","Round-trip test documents containing xml:space."],"tags":["xaml","xml-space","unsupported-operation"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}