You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/standard/linq/preserve-white-space-serializing.md
+78Lines changed: 78 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,3 +25,81 @@ The following methods in the <xref:System.Xml.Linq.XElement> and <xref:System.Xm
25
25
If the method doesn't take <xref:System.Xml.Linq.SaveOptions> as an argument, then the method will format (indent) the serialized XML. In this case, all insignificant white space in the XML tree is discarded.
26
26
27
27
If the method does take <xref:System.Xml.Linq.SaveOptions> as an argument, then you can specify that the method not format (indent) the serialized XML. In this case, all white space in the XML tree is preserved.
28
+
29
+
## Roundtripping XML with carriage return entities
30
+
31
+
The whitespace preservation discussed in this article is different from XML roundtripping. When XML contains carriage return entities (`
`), LINQ to XML's standard serialization might not preserve them in a way that allows perfect roundtripping.
32
+
33
+
Consider the following example XML that contains carriage return entities:
34
+
35
+
```xml
36
+
<xxml:space="preserve">a
37
+
b
38
+
c
</x>
39
+
```
40
+
41
+
When you parse this XML with `XDocument.Parse()`, the root element's value becomes `"a\r\nb\nc\r"`. However, if you reserialize it using LINQ to XML methods, the carriage returns are not entitized:
The values are different: the original was `"a\r\nb\nc\r"` but after roundtripping it becomes `"a\nb\nc\n"`.
64
+
65
+
### Solution: Use XmlWriter with NewLineHandling.Entitize
66
+
67
+
To achieve true XML roundtripping that preserves carriage return entities, use <xref:System.Xml.XmlWriter> with <xref:System.Xml.XmlWriterSettings.NewLineHandling> set to <xref:System.Xml.NewLineHandling.Entitize>:
68
+
69
+
```csharp
70
+
stringxmlWithCR="""<x xml:space="preserve">a
71
+
b
72
+
c
</x>""";
73
+
74
+
XDocumentdoc=XDocument.Parse(xmlWithCR);
75
+
76
+
// Create XmlWriter settings with NewLineHandling.Entitize
77
+
XmlWriterSettingssettings=newXmlWriterSettings
78
+
{
79
+
NewLineHandling=NewLineHandling.Entitize,
80
+
OmitXmlDeclaration=true
81
+
};
82
+
83
+
// Serialize using XmlWriter
84
+
usingStringWriterstringWriter=newStringWriter();
85
+
using (XmlWriterwriter=XmlWriter.Create(stringWriter, settings))
Console.WriteLine($"Values match after roundtripping: {valuesMatch}");
100
+
// Output: True
101
+
```
102
+
103
+
When you need to preserve carriage return entities for XML roundtripping, use <xref:System.Xml.XmlWriter> with the appropriate <xref:System.Xml.XmlWriterSettings> instead of LINQ to XML's built-in serialization methods.
104
+
105
+
For more information about <xref:System.Xml.XmlWriter> and its settings, see <xref:System.Xml.XmlWriter?displayProperty=fullName>.
0 commit comments