Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are all of my line-breaks changing from "/r/n" to "/n/" and how can I stop this from happening?

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

like image 624
Justin Avatar asked Jul 14 '10 00:07

Justin


2 Answers

You can use XmlWriterSettings to control what your line-break characters are:

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

Whatever you're using to read in your XML might be normalizing your line endings.

like image 185
Cᴏʀʏ Avatar answered Sep 21 '22 06:09

Cᴏʀʏ


If you set the PreserveWhiteSpace property on your XmlDocument object before calling Load() and Save() then this will not happen:

var doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.Load("foo.xml");
...
doc.Save("bar.xml"); // Line endings will not be altered
like image 42
d4nt Avatar answered Sep 20 '22 06:09

d4nt