我在获取SOAP服务以反序列化对象数组时遇到问题;它唯一的读数在数组的第一个成员,没有别的。这是我的代码:
public void StoreCredentials(Credentials credentials)
{
Credentials[] credsArray;
var soap = new SoapFormatter();
var stream = new FileStream(_path, FileMode.OpenOrCreate);
try
{
credsArray = (Credentials[])soap.Deserialize(stream);
var credsList = credsArray.ToList();
credsList.Add(credentials);
credsArray = credsList.ToArray();
}
catch (SerializationException)
{
credsArray = new[] {credentials};
}
soap.Serialize(stream, credsArray);
stream.Close();
}
我写了一个简单的单元测试,它将两个Credentials对象添加到文件中,输出看起来正确,两组凭据都存在,但是当我运行测试将第三个集添加到文件时,soap.Deserialize( stream)line返回一个只有一个条目的数组,即使它的读取文件包含两个条目。我在这里做错了吗?有没有更好/更简单的方法来做到这一点?请帮忙!
答案 0 :(得分:0)
我明白了。问题在于FileStream的FileMode.OpenOrCreate属性。这将打开文件,但附加一个新集合,而不是更改原始集合。我需要使用FileMode.Create覆盖该文件,所以这里是工作代码(我也将集合更改为Hashtable,效果更好):
public void StoreCredentials(Credentials credentials)
{
credentials.Encrypt(_myUser.Encryption);
Hashtable credsTable;
var soap = new SoapFormatter();
FileStream stream;
try
{
stream = new FileStream(_path, FileMode.Open, FileAccess.ReadWrite);
credsTable = (Hashtable) soap.Deserialize(stream);
stream.Close();
stream = new FileStream(_path, FileMode.Create, FileAccess.ReadWrite);
if (credsTable.ContainsKey(credentials.Id))
credsTable[credentials.Id] = credentials;
else
credsTable.Add(credentials.Id, credentials);
}
catch (FileNotFoundException e)
{
stream = new FileStream(_path, FileMode.Create, FileAccess.ReadWrite);
credsTable = new Hashtable {{credentials.Id, credentials}};
}
soap.Serialize(stream, credsTable);
stream.Close();
}