1

我正在使用 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()的 CRUD

public 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.");
        }
    }

这是我的持久性控制器

 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();
    }
}
4

1 回答 1

1

我想你的问题可能是FileMode.OpenOrCreate。做到这一点,FileMode.Create否则您将从原始(较长)文件中获得不需要的尾巴。

并且,副话题,确实使用using() {}块来处理 FileStreams。

于 2012-03-21T21:06:48.983 回答