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?
views:
58answers:
1
+2
Q:
Why are all of my line-breaks changing from "/r/n" to "/n/" and how can I stop with from happening?
+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
2010-07-14 00:41:07
It is still not working. I am using XDocument.Load(path) to load the file.
Justin
2010-07-14 00:54:51
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
2010-07-14 03:44:17
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
2010-07-14 03:59:07