public override void Save(Stream stream) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } XmlWriter xmlWriter = null; try { if (PreserveWhitespace) { XmlFormatter.Format(this); xmlWriter = new XmlAttributePreservingWriter(stream, TextEncoding); } else { XmlTextWriter textWriter = new XmlTextWriter(stream, TextEncoding) { Formatting = Formatting.Indented }; xmlWriter = textWriter; } WriteTo(xmlWriter); } finally { xmlWriter?.Flush(); } }
public override void Save(string filename) { if (string.IsNullOrWhiteSpace(filename)) { throw new ArgumentException("File name cannot be empty", nameof(filename)); } XmlWriter xmlWriter = null; try { if (PreserveWhitespace) { XmlFormatter.Format(this); xmlWriter = new XmlAttributePreservingWriter(filename, TextEncoding); } else { var textWriter = new XmlTextWriter(filename, TextEncoding) { Formatting = Formatting.Indented }; xmlWriter = textWriter; } WriteTo(xmlWriter); } finally { if (xmlWriter != null) { xmlWriter.Flush(); xmlWriter.Close(); } } }