Description
If I have XML that looks like this:
<x xml:space="preserve">a
b
c
</x>
and then parse it with XDocument.Parse()
, I get this string as expected for the value of the root node (XDocument.Root.Value
): "a\r\nb\nc\r"
. But if I reserialize it—even with SaveOptions.DisableFormatting
(which makes no difference since I set xml:space="preserve"
in the document itself)—I get a different document out:
<x xml:space="preserve">a
b
c</x>
…and upon reparsing, the value of the root node is now a different string: "a\nb\nc\n"
.
I’d expect the document describing whitespace preservation to discuss or link to a guide on how to round trip XML. Or it could at least describe that the “whitespace preservation” described in this document is different than round-tripping XML and mention that the only way to round trip XML is to use an XmlWriter
with new XmlWriterSettings { NewLineHandling = NewLineHandling.Entitize, }
.
See my other remarks at https://stackoverflow.com/q/45284814.
EDIT: Manually fixed weird partial entitization in fenced code from the docs.microsoft.com issue creation form.
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 818199d0-63ff-dac5-a71e-d4378e495317
- Version Independent ID: 663557c7-6c36-9961-8366-87411610f74f
- Content: Preserving White Space While Serializing3
- Content Source: docs/csharp/programming-guide/concepts/linq/preserving-white-space-while-serializing.md
- Product: dotnet-csharp
- GitHub Login: @BillWagner
- Microsoft Alias: wiwagn