문제

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!

도움이 되었습니까?

해결책

You need to create an XmlWriter:

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

다른 팁

var xml = "<?xml version= ...";  //your xml string
using (var w = XmlWriter.Create(path,
                    new XmlWriterSettings
                    {
                        NewLineChars = "\n",
                    })
)
{
    w.WriteRaw(xml);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top