Next line sometimes is corrupting xml file and creating a zero length xml, when OutOfMemoryException has been thrown. How to prevent file corruption?
xmlDoc.Save(filename)
Save-Exception of type ‘System.OutOfMemoryException’ was thrown.
Save-at System.IO.FileStream.Write(Byte[] array, Int32 offset, Int32
count) at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean
flushEncoder) at System.IO.StreamWriter.Write(Char value) at
System.Xml.XmlTextWriter.Indent(Boolean beforeEndElement) at
System.Xml.XmlTextWriter.AutoComplete(Token token) at
System.Xml.XmlTextWriter.WriteStartElement(String prefix, String
localName, String ns) at
System.Xml.XmlDOMTextWriter.WriteStartElement(String prefix, String
localName, String ns) at System.Xml.XmlElement.WriteTo(XmlWriter w)
at System.Xml.XmlElement.WriteContentTo(XmlWriter w) at
System.Xml.XmlElement.WriteTo(XmlWriter w) at
System.Xml.XmlDocument.WriteContentTo(XmlWriter xw) at
System.Xml.XmlDocument.WriteTo(XmlWriter w) at
System.Xml.XmlDocument.Save(String filename) at
MainOptions.Save(String filename, ItemOptions options)
You’re overwritting the file as you save it to
filename.To backup the old copy,
filename.bak) before saving anddelete it afterwards or
filename.new) and rename it on successWhen an exception is thrown, you can easily restore the old/previous version of the xml file.