Writing XMLDocument to file with specific newline character (c#)

后端 未结 2 637
情歌与酒
情歌与酒 2021-01-18 01:00

I have an XMLDocument that I have read in from file. The file is Unicode, and has the newline character \'\\n\'. When I write the XMLDocument back out, it has the newline ch

2条回答
  •  -上瘾入骨i
    2021-01-18 01:53

    Use XmlWriter.Create() to create the writer and specify the format. This worked well:

    using System;
    using System.Xml;
    
    class Program {
        static void Main(string[] args) {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.NewLineChars = "\n";
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create(@"c:\temp\test.xml", settings);
            XmlDocument doc = new XmlDocument();
            doc.InnerXml = "value";
            doc.WriteTo(writer);
            writer.Close();
        }
    }
    

提交回复
热议问题