Question

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!

Was it helpful?

Solution

You need to create an XmlWriter:

using (var w = XmlWriter.Create(path, new XmlWriterSettings { 
        NewLineChars = "\n", 
    }))
{
    xml.Save(w);
}

OTHER TIPS

var xml = "<?xml version= ...";  //your xml string
using (var w = XmlWriter.Create(path,
                    new XmlWriterSettings
                    {
                        NewLineChars = "\n",
                    })
)
{
    w.WriteRaw(xml);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top