Why do all my line breaks change from "/ r / n" to "/ n /" and how can I stop this?

I save my files as XML documents using XDocument.Save (path), and after saving and loading the document, all line breaks changed from "/ r / n" to "/ n /". Why is this happening and how can I fix it?

+5
source share
2 answers

You can use XmlWriterSettingsline breaks to control your characters:

XmlWriterSettings xws = new XmlWriterSettings();
xws.NewLineChars = "\r\n";
using (XmlWriter xw = XmlWriter.Create("whatever.xml", xws))
{
   xmlDocumentInstance.Save(xw);
}

No matter what you use to read in your XML, you can normalize line endings.

+5
source

PreserveWhiteSpace XmlDocument Load() Save(), :

var doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.Load("foo.xml");
...
doc.Save("bar.xml"); // Line endings will not be altered
+1

All Articles