dotnet/wpf · error · XamlXmlWriterException

SR.CannotWriteXmlSpacePreserveOnMember (member, value)

Error message

SR.CannotWriteXmlSpacePreserveOnMember (member, value)

What it means

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.

Solutions

  1. Move xml:space="preserve" from the property/member onto the containing object element.
  2. Remove xml:space="preserve" and normalize the whitespace in the value.
  3. Catch XamlXmlWriterException and fall back to writing the value without preserve.
  4. Restructure the node stream so the preserved value is written under an object frame.

Example fix

<!-- before -->
<TextBlock><TextBlock.Text xml:space="preserve">  hi  </TextBlock.Text></TextBlock>
<!-- after -->
<TextBlock xml:space="preserve"><TextBlock.Text>  hi  </TextBlock.Text></TextBlock>
Defensive patterns

Strategy: try-catch

Validate before calling

// before writing a preserved value, confirm xml:space is on an object element, not a member element

Try / catch

try { writer.WriteValue(preserved); } catch (XamlXmlWriterException ex) when (ex.Message.Contains("xml:space")) { /* drop preserve or move directive */ }

Prevention

When it happens

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

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

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/199e932ccfc23188. Report an issue: GitHub.

Appendix: source

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

                    else
                    {
                        writer.output.WriteValue(value);
                        writer.currentState = InMemberAfterValue.State;
                    }
                }

                if (writer.currentState != InMemberAfterValueWithSignificantWhitespace.State)
                {
                    writer.isFirstElementOfWhitespaceSignificantCollection = false;
                }
            }

            private void WriteXmlSpaceOrThrow(XamlXmlWriter writer, string value)
            {
                var frameWithXmlSpacePreserve = FindFrameWithXmlSpacePreserve(writer);
                if (frameWithXmlSpacePreserve.AllocatingNodeType == XamlNodeType.StartMember)
                {
                    throw new XamlXmlWriterException(SR.Format(SR.CannotWriteXmlSpacePreserveOnMember, frameWithXmlSpacePreserve.Member, value));
                }

                WriteXmlSpace(writer);
            }

            // this method finds the SO or SM where "xml:space = preserve" will actually be attached to
            private Frame FindFrameWithXmlSpacePreserve(XamlXmlWriter writer)
            {
                var frameEnumerator = writer.namespaceScopes.GetEnumerator();

                while (frameEnumerator.MoveNext())
                {
                    var frame = frameEnumerator.Current;
                    if (frame.AllocatingNodeType == XamlNodeType.GetObject)
                    {
                        continue;
                    }

View on GitHub (pinned to 81131a70a4)