Is there any way to save an XmlDocument *without* indentation and line returns?

后端 未结 2 1387
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-19 13:26

All my searches have brought up people asking the opposite, but I have a file which grows by nearly 50% if it is saved with line returns and indentation.

Is there any wa

相关标签:
2条回答
  • 2021-02-19 13:49

    Use XmlWriterSettings:

    XmlDocument xmlDoc = new XmlDocument();
    [...]
    XmlWriterSettings xwsSettings = new XmlWriterSettings();
    xwsSettings.Indent = false;
    xwsSettings.NewLineChars = String.Empty;
    using (XmlWriter xwWriter = XmlWriter.Create(@"c:\test.xml", xwsSettings))
     xmlDoc.Save(xwWriter);
    
    0 讨论(0)
  • 2021-02-19 14:08

    Try this code:

    XmlDocument doc = new XmlDocument();
    
    using(XmlTextWriter wr = new XmlTextWriter(fileName, Encoding.UTF8))
    {
        wr.Formatting = Formatting.None; // here's the trick !
        doc.Save(wr);
    }
    
    0 讨论(0)
提交回复
热议问题