나는 Datacontractserializer를 사용하여 객체를 파일에 다시 저장하면 새 xml의 길이가 원래 파일의 xml보다 짧을 때 원본 xml의 나머지 부분 outwith 새로운 xml의 길이는 파일에 남아 XML을 깨뜨릴 것입니다.Datacontractserializer가 모든 데이터를 덮어 쓰지 않는다
아무에게도이 문제를 해결할 수있는 좋은 해결책이 있습니까? 나는이 FileMode.OpenOrCreate
를 사용하여 때문이라고 생각
/// <summary>
/// Flushes the current instance of the given type to the datastore.
/// </summary>
private void Flush()
{
try
{
string directory = Path.GetDirectoryName(this.fileName);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
FileStream stream = null;
try
{
stream = new FileStream(this.fileName, FileMode.OpenOrCreate);
for (int i = 0; i < 3; i++)
{
try
{
using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(stream, new System.Text.UTF8Encoding(false)))
{
stream = null;
// The serializer is initialized upstream.
this.serializer.WriteObject(writer, this.objectValue);
}
break;
}
catch (IOException)
{
Thread.Sleep(200);
}
}
}
finally
{
if (stream != null)
{
stream.Dispose();
}
}
}
catch
{
// TODO: Localize this
throw;
//throw new IOException(String.Format(CultureInfo.CurrentCulture, "Unable to save persistable object to file {0}", this.fileName));
}
}
건배! 그걸 수정 한 것 같습니다. 지난 며칠 동안 나는 이것에 시달렸습니다. 필자는 FileMode 옵션으로 충분히 자세히 보지 않았습니다. –