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 4611102
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:13:21+00:00 2026-05-22T01:13:21+00:00

In relation to my other question, I have serialized data and then deserialized it,

  • 0

In relation to my other question, I have serialized data and then deserialized it, but when I write new data to the deserialized arraylist, it does not add the new data to the end of the arraylist. Instead it is overwriting previous list data. Any help would be great. Here’s my code so far:

    {
        ArrayList siteList = new ArrayList();
        ArrayList copy = new ArrayList(siteList);

        //if previous data exists, deserialize it
        try
        {
            ArrayList deserializedArray = DeserializeArray();

            foreach (var item in deserializedArray)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("------");
        }
        catch
        {
            Console.WriteLine("No previous data found");
        }
        siteList.Add("Test 3");
        siteList.Add("Test 4");

        //Display all current items in list
        foreach (var item in siteList)
        {
            Console.WriteLine(item);
        }
        SerializeArray(siteList);
        Console.Read();
    }

    public static void SerializeArray(ArrayList siteList)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(ArrayList));
        TextWriter textWriter = new StreamWriter("SiteList.xml");
        serializer.Serialize(textWriter, siteList);
        textWriter.Close();
    }

    static ArrayList DeserializeArray()
    {
        XmlSerializer deserializer = new XmlSerializer(typeof(ArrayList));
        TextReader textReader = new StreamReader("SiteList.xml");
        ArrayList siteList;
        siteList = (ArrayList)deserializer.Deserialize(textReader);
        textReader.Close();

        return siteList;
    }
}

}

  • 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-22T01:13:22+00:00Added an answer on May 22, 2026 at 1:13 am

    You never actually assign the data from the deserialized arraylist to siteList which is the variable that you try to update later – assign it directly:

    ArrayList siteList = DeserializeArray();
    

    and update your DeserializeArray() method to take care of the case that no prior XML exists:

    static ArrayList DeserializeArray()
    {
        if(!File.Exists("SiteList.xml"))
            return new ArrayList();
    
        XmlSerializer deserializer = new XmlSerializer(typeof(ArrayList));
        TextReader textReader = new StreamReader("SiteList.xml");
        ArrayList siteList = (ArrayList)deserializer.Deserialize(textReader);
        textReader.Close();
    
        return siteList;
    }
    

    In general it is a bad idea to control flow with exceptions, especially in this case when you can just check whether the file exists or not.

    The final method would look something like this then (some fluff removed):

    static void Main(string[] args)
    {
        ArrayList siteList = DeserializeArray();
        siteList.Add("Test 3");
        siteList.Add("Test 4");
        foreach (var item in siteList)
        {
            Console.WriteLine(item);
        }
        SerializeArray(siteList);
        Console.Read();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For my new Pet-Project I have a question for design, that is decided already,
Hey folks, I have a question which feels stupid but I can't quite say
In relation to this question on Using OpenGL extensions , what's the purpose of
In relation to another question , how do you account for paths that may
This is in relation to this question I am hosting this WCF service in
I have a relation mapping table like this: attributeid bigint productid bigint To clean
when i have a many-to.many relation with nhibernate and let nhibernate generate my db
I have two models: Person and Relation. The second one stores information about relations
I have a question, and I'm going to tag this subjective since that's what
I have a question that is more in the realm of design, than implementation.

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.