Preserve empty blank lines after loading string into XmlDocument

45 Views Asked by At

I have these rather large strings that contain several linebreaks "\r\n" and, in some occasions I may even have sequences of linebreaks "\r\n\r\n...". This results in empty lines, which I do want to preserve upon loading the string into a XmlDocument object via LoadXml().

By default, empty lines are removed.

I've managed to find a way to keep those blank lines by using XDocument, but I still want to check if there is a way to do that with XmlDocument exclusively (so as to avoid to refactor big chunks of my code that rely in XmlDocument)

1

There are 1 best solutions below

0
Mads Hansen On

Use the XmlDocument.PreserveWhitespace Property

https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmldocument.preservewhitespace?view=net-8.0

If PreserveWhitespace is true before Load or LoadXml is called, white space nodes are preserved; otherwise, if this property is false, significant white space is preserved, white space is not.