受保护的bool WriteConfigFile(字符串文件名,类型类型,对象obj)
bool retValue=true;
FileStream fileStream = null;
XmlSerializer xmlSerializer = null;
try
{
this.BackupFile(fileName);
fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
xmlSerializer = new XmlSerializer(type);
xmlSerializer.Serialize(fileStream, obj);
}
catch (Exception ex)
{
retValue = false;
//LogManager.SingleInstance.WriteLog(this, ex.ToString());
}
finally
{
if (fileStream != null)
{
fileStream.Close();
}
if (retValue== false)
{
this.RestoreBackupFile(fileName);
}
this.DeleteBackupFile(fileName);
}
return retValue;
}
主要问题是xml不是使用序列化形成的。一旦我这么做了,这种反序列化就很好了。序列化后,它将使用转义符号自动保存。