views:

58

answers:

1

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?

+3  A: 

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.

Cory Larson
It is still not working. I am using XDocument.Load(path) to load the file.
Justin
Do you need line breaks at all? Could you just save and load the XML while completely ignoring whitespace? Maybe we're trying to solve the wrong problem.
Cory Larson
I do need the line breaks. The xml file that I am working with is a custom filetype for the text editor that I'm working on.I was able to solve my problem by using these settings:`xws.NewLineChars = Environment.NewLine;xws.NewLineHandling = NewLineHandling.Replace;`However, I still don't understand what was going on.
Justin