Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save XDocument with UNIX line endings

I have a code section looking like this:

XDocument xml = new XDocument(
    new XElement("test1",
        new XElement("test2", "abc")
    )
);

I now want to save the xml document using the Save method:

xml.Save("test.xml");

Then I took a look at the file using a hex editor and noticed that it has windows line endings (/r/n). However, I "only" need UNIX line endings (/n).

Hexeditor showing line endings

Thanks in advance!

like image 731
ltsstar Avatar asked Jun 09 '13 20:06

ltsstar


1 Answers

You need to create an XmlWriter:

using (var w = XmlWriter.Create(path, new XmlWriterSettings { 
        NewLineChars = "\n", 
    }))
{
    xml.Save(w);
}
like image 68
SLaks Avatar answered Oct 01 '22 21:10

SLaks