Skip to content

Commit

Permalink
Merge branch 'stable'
Browse files Browse the repository at this point in the history
* stable:
  (GH-355) Write config to temp path first
  (maint) formatting
  • Loading branch information
ferventcoder committed Sep 18, 2015
2 parents d65471b + acf7262 commit aeb856b
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions src/chocolatey/infrastructure/services/XmlService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,42 +38,47 @@ public XmlType deserialize<XmlType>(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);
}

public void serialize<XmlType>(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);
}
}
}
}

0 comments on commit aeb856b

Please sign in to comment.