diff --git a/src/chocolatey/infrastructure/services/XmlService.cs b/src/chocolatey/infrastructure/services/XmlService.cs index 3008165215..18bcd8d577 100644 --- a/src/chocolatey/infrastructure/services/XmlService.cs +++ b/src/chocolatey/infrastructure/services/XmlService.cs @@ -38,18 +38,18 @@ public XmlType deserialize(string xmlFilePath) { return FaultTolerance.try_catch_with_logging_exception( () => + { + var xmlSerializer = new XmlSerializer(typeof(XmlType)); + var xmlReader = XmlReader.Create(new StringReader(_fileSystem.read_file(xmlFilePath))); + if (!xmlSerializer.CanDeserialize(xmlReader)) { - var xmlSerializer = new XmlSerializer(typeof (XmlType)); - var xmlReader = XmlReader.Create(new StringReader(_fileSystem.read_file(xmlFilePath))); - if (!xmlSerializer.CanDeserialize(xmlReader)) - { - this.Log().Warn("Cannot deserialize response of type {0}", typeof (XmlType)); - return default(XmlType); - } + this.Log().Warn("Cannot deserialize response of type {0}", typeof(XmlType)); + return default(XmlType); + } - return (XmlType) xmlSerializer.Deserialize(xmlReader); - }, - "Error deserializing response of type {0}".format_with(typeof (XmlType)), + return (XmlType)xmlSerializer.Deserialize(xmlReader); + }, + "Error deserializing response of type {0}".format_with(typeof(XmlType)), throwError: true); } @@ -57,23 +57,28 @@ public void serialize(XmlType xmlType, string xmlFilePath) { _fileSystem.create_directory_if_not_exists(_fileSystem.get_directory_name(xmlFilePath)); + var xmlUpdateFilePath = xmlFilePath + ".update"; + FaultTolerance.try_catch_with_logging_exception( () => + { + var xmlSerializer = new XmlSerializer(typeof(XmlType)); + var textWriter = new StreamWriter(xmlUpdateFilePath, append: false, encoding: Encoding.UTF8) { - var xmlSerializer = new XmlSerializer(typeof (XmlType)); - var textWriter = new StreamWriter(xmlFilePath, append: false, encoding: Encoding.UTF8) - { - AutoFlush = true - }; + AutoFlush = true + }; + + xmlSerializer.Serialize(textWriter, xmlType); + textWriter.Flush(); - xmlSerializer.Serialize(textWriter, xmlType); - textWriter.Flush(); + textWriter.Close(); + textWriter.Dispose(); - textWriter.Close(); - textWriter.Dispose(); - }, - "Error serializing type {0}".format_with(typeof (XmlType)), + _fileSystem.copy_file(xmlUpdateFilePath, xmlFilePath, overwriteExisting: true); + _fileSystem.delete_file(xmlUpdateFilePath); + }, + "Error serializing type {0}".format_with(typeof(XmlType)), throwError: true); } } -} \ No newline at end of file +}