Prevent file corruption while saving xml file

The next line sometimes distorts the xml file and creates a zero xml size when an OutOfMemoryException is thrown. How to prevent file corruption?

xmlDoc.Save(filename) 

A save-exception of type 'System.OutOfMemoryException' has been thrown. Save to System.IO.FileStream.Write (Byte array [], Int32 offset, Int32 count) in System.IO.StreamWriter.Flush (Boolean flushStream, Boolean flushEncoder) in System.IO.StreamWriter.Write (Char value) in System .Xml String, String localName, String ns) in System.Xml.XmlElement.WriteTo (XmlWriter w) in System.Xml.XmlElement.WriteContentTo (XmlWriter w) in System.Xml.XmlElement.WriteTo (XmlWriter w) in System.Xml.XmlDocument .WriteContentTo (XmlWriter xw) in System.Xml.XmlDocument.WriteTo (XmlWriter w) in System.Xml.XmlDocument.Save (String filename) in MainOptions.Save (file parameters String, ItemOptions)

+4
source share
4 answers

You overwrite the file when saving it to filename . To back up an old copy,

  • move it to another location (e.g. filename.bak ) before saving and delete it later or
  • save the new file as another file (e.g. filename.new ) and rename it on success

When an exception is thrown, you can easily restore the old / previous version of the xml file.

+6
source

Agree with Matten . An alternative solution might generate an xml string before saving to a file:

 Using ms As New MemoryStream xmlDoc.Save(ms) Using outStream As FileStream = File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.Read) ms.WriteTo(outStream) End Using End Using 

Use stream to match xmlDoc.Save (file name)

+2
source

OutOfMemory exception

This exception may be caused by the general language version when there is not enough memory to allocate for internal purposes or new instances of the object. To avoid this exception, avoid programming large methods that consume 64 or more kilobytes of memory.

Thus, one of the possible reasons may be the method in which you save your XML document, trying to allocate more than 64 KB.

To avoid corruption, the answer is "Matten" is good enough, and Jon skeet offers the same here .

But I want to add additional security checks using locks during this operation.

  private static readonly object locker = new object(); public static void OnlyOneCallerAllowed() { lock (locker) { string tempFileName = System.IO.Path.GetTempFileName(); xmlDoc.Save(tempFileName); File.Move(original_file, original_file.old); File.Move(tempFileName,original_file); File.Delete( tempFileName); } } 
+1
source

OutOfMemory exception addressing ...

If the document is large and has children at several levels, indentation can inflate the file size. I found that an XmlDocument indented with XmlTextWriter uses spaces. I AM

See: http://msdn.microsoft.com/en-us/library/system.xml.xmltextwriter.formatting%28v=vs.71%29.aspx

Try turning off all padding and see if that helps. If it takes into account fingerprint 1 and file restructuring.

0
source

Source: https://habr.com/ru/post/1388423/


All Articles