我正在使用NetDataContractSerializer。我可以创建,添加和序列化对象到数据文件没问题;将文件重新加载到GUI中没有问题。
但是当我尝试从数据中删除(删除)对象并重新保存(序列化)数据时,我遇到了问题。
这是我的删除房间按钮;当用户请求删除特定楼层上的房间时,程序将创建驻留在该特定楼层上的特定房间内的对象列表。然后删除它们,关闭它们正在查看的当前表单并保存数据。
这似乎有效,因为程序没有崩溃,房间从当前楼层的房间列表中移除。
但是当我尝试重新加载文件(关闭程序,打开并加载)时,我收到此错误:
“反序列化对象时出错。根级别的数据无效。第1行,第1位。”
这是我的deleteRoom按钮
private void btn_deleteRoom_Click(object sender, EventArgs e)
{
var assets = getAssetsForCurrentRoom();
string warningMessage = "Deleting this room will delete this room and all contained assets! Are you sure you want to do this?";
string caption = "WARNING!";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;
//Display the MessageBox
result = MessageBox.Show(warningMessage, caption, buttons);
if (result == System.Windows.Forms.DialogResult.Yes)
{
var itemsToRemove = new ArrayList();
foreach (var item in currentHouse.GetAssets())
{
if (item.Parent.Name == currentRoom.Name)
{
itemsToRemove.Add(item);
}
}
foreach (var item in itemsToRemove)
{
currentHouse.deleteAsset((Asset)item);
}
currentHouse.DeleteRoom(currentRoom);
PersistanceController.SaveHouseWithCurrentPath(currentHouse);
this.Close();
}
}
以下是 deleteAsset()
方法的CRUDpublic void deleteAsset(Asset asset)
{
//is null?
if (asset == null)
{
throw new ArgumentNullException("asset", "Asset cannot be null.");
}
//is blank
if (string.IsNullOrWhiteSpace(asset.Name))
{
throw new ArgumentNullException("asset", "Asset name cannot be blank | null.");
}
var listAsset = _assets.FirstOrDefault(existingAsset => (existingAsset.Name == asset.Name));
if (listAsset != null)
{
_assets.Remove(asset);
}
else
{
throw new InvalidOperationException("Asset does not exist; thus it can not be deleted.");
}
}
这是我的PersistanceController
public static class PersistanceController
{
public static string LastLoadedPath { get; set; }
public static House LoadHouse(string path)
{
NetDataContractSerializer houseDeserializer = new NetDataContractSerializer();
FileStream houseFileStream = new FileStream(path, FileMode.Open);
House deserialzedHouse = (House)houseDeserializer.Deserialize(houseFileStream);
houseFileStream.Close();
LastLoadedPath = path;
return deserialzedHouse;
}
public static void SaveHouseWithCurrentPath(House house)
{
SaveHouse(house, LastLoadedPath);
}
public static void SaveHouse(House house, string path)
{
//save house
NetDataContractSerializer xmlSerializer = new NetDataContractSerializer();
Stream streamWriter = new FileStream(path, FileMode.OpenOrCreate);
xmlSerializer.Serialize(streamWriter, house);
streamWriter.Close();
}
}
答案 0 :(得分:2)
我认为您的问题可能是FileMode.OpenOrCreate
。只需FileMode.Create
,或者您将从原始(较长)文件中获得不需要的尾部。
而且,侧面主题,请使用using() {}
块来处理FileStreams。