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(); } }
internal void WritePreservedAttributes(XmlAttributePreservingWriter writer, XmlAttributeCollection attributes) { string?oldNewLineString = null; if (_attributeNewLineString is not null) { oldNewLineString = writer.SetAttributeNewLineString(_attributeNewLineString); } try { foreach (string attributeName in _orderedAttributes) { XmlAttribute attr = attributes[attributeName]; if (attr is null) { continue; } if (_leadingSpaces.ContainsKey(attributeName)) { writer.WriteAttributeWhitespace(_leadingSpaces[attributeName]); } attr.WriteTo(writer); } if (_leadingSpaces.ContainsKey(string.Empty)) { writer.WriteAttributeTrailingWhitespace(_leadingSpaces[string.Empty]); } } finally { if (oldNewLineString is not null) { writer.SetAttributeNewLineString(oldNewLineString); } } }
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(); } } }
void WritePreservedAttributesTo(XmlAttributePreservingWriter preservingWriter) => _preservationDict.WritePreservedAttributes(preservingWriter, Attributes);