XML output XML will lose line breaks

I am trying to add a comment to an existing XML document. The document opened in the XML reader (I use notepad ++) is perfectly formatted. However, when I run it through my code, which simply adds a comment to the top, the XML comes out as one long line. In other words, all line breaks have been deleted. How do I maintain line breaks?

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(doc.Uri);

//This is where I add comments to a string

xmlDoc.DocumentElement.PrependChild(enrichedDoc.CreateComment(comments));

return xmlDoc;
+3
source share
2 answers
xmlDoc.PreserveWhitespace = true;

That should do the trick.

+5
source

You are looking for the PreserveWhitespace property :

xmlDoc.PreserveWhitespace = true;
0
source

Source: https://habr.com/ru/post/1774997/


All Articles