Skip to content

Missing discussion on how to roundtrip XML with carriage return entities #9680

@binki

Description

@binki

If I have XML that looks like this:

<x xml:space="preserve">a&#xd;
b
c&#xd;</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.


Associated WorkItem - 446784

Metadata

Metadata

Labels

📌 seQUESTeredIdentifies that an issue has been imported into Quest.csharp-linq/subsvcdotnet-csharp/svcin-prThis issue will be closed (fixed) by an active pull request.okr-qualityContent-quality KR: Concerns article defects (bugs), freshness, or build warnings.

Type

No type

Projects

Status

✅ Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions