Skip to content

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

Open
@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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions