Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7664677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:21:55+00:00 2026-05-31T14:21:55+00:00

I am using the NetDataContractSerializer. I can create, add and serialize objects into data

  • 0

I am using the NetDataContractSerializer. I can create, add and serialize objects into data file no problem; reloading the file into the GUI has no issue.

However I run into a problem when I attempt to delete(remove) objects from the data and resave (serialize) the data.

Here is my delete room button; when a user requests the deletion of a room on a particular floor, the program will create a list of objects that reside within that particular room on that particular floor. Then delete them, close the current form they are viewing and save the data.

This seems to work, as the program does not crash and the room is removed from the list of rooms on the current floor.

However when I attempt to reload the file ( close program, open and load) I get this error:

“There was an error deserializing the object . The data at the root level is invalid. Line 1, position 1.”

Here is my deleteRoom Button

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

Here is my CRUD for the method deleteAsset()

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

Here is my 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();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:21:57+00:00Added an answer on May 31, 2026 at 2:21 pm

    I think your problem might beFileMode.OpenOrCreate. Make that just FileMode.Create or you’ll have an unwanted tail from the original (longer) file.

    And, side topic, do use using() {} blocks to work with FileStreams.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Github's Issue Tracker, how can one create an issue tied to an organization
Using the HTML5 File API I can get the Binary String representation of a
Using mercurial, I've run into an odd problem where a line from one committer
Using Android TelephonyManager an application can obtain the state of data activity over the
I have a client-server application, which communicates using WCF, and uses NetDataContractSerializer to serialize
I have some events in a class. While I serialize them using NetDataContractSerializer, the
I'm using NetDataContractSerializer to exchange data among applications. I would like the ReadObject method
I am using a netdatacontractserializer and a SerializationBinder to create my own runtime types
I have a situation where I'm serializing some .NET objects using NetDataContractSerializer and storing
I'm using NetDataContractSerializer . After successfully deserializing an object, is there a way to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.