0

I've got this code.

        const int maxbooks = 5;
        Book[] booklist = new Book[maxbooks];

        FileStream fs = File.Open(@"books.txt", FileMode.Open, FileAccess.Read);
        SoapFormatter sf = new SoapFormatter();

        try
        {

           // something here, deserializing file and assigning to the array

        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        finally
        {
            fs.Close();
        }

I've figured out (or at least, I THINK I've figured out) how to serialize the original array of objects in a separate program. I'm now looking to de serialize that, and create a new array with the de serialized data. Just for reference, here's the other file where I serialized the original array

        Book firstbook = new Book("Jimbob", "Jimbob book", "first edition", "Jimbob publishing", "1991");
        Book secondbook = new Book("Greg", "Greg book", "third edition", "Unholy publishing", "2010");
        Book thirdbook = new Book("Pingu", "Pingu book", "tenth edition", "Antarctic publishing", "1897");
        Book fourthbook = new Book("Patrick", "Patrick book", "seventh edition", "underwater publishing", "1991");
        Book fifthbook = new Book("Sally", "Sally book", "first edition", "Wowpublishing", "2015");


        const int maxbooks = 5;

        Book[] booklist = new Book[maxbooks];
        booklist[0] = firstbook;
        booklist[1] = secondbook;
        booklist[2] = thirdbook;
        booklist[3] = fourthbook;
        booklist[4] = fifthbook;


        // writing to a file

        FileStream fs = File.Open(@"books.txt", FileMode.Create, FileAccess.Write);
        SoapFormatter sf = new SoapFormatter();

        int bookindex = 0;

        try
        {
            while (bookindex < maxbooks)
            {
                sf.Serialize(fs, booklist[bookindex]);
                bookindex += 1;
            }
        }

        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
       finally
        {
            fs.Close();
        }

Using SOAP serialization for the moment with this. Any help will be appreciated.

iskelz
  • 65
  • 1
  • 3

2 Answers2

1

Serialise the array itself rather than per item. With your serialiser routine you are creating many serialisation chunks that are not valid together appended in a file.

James Lucas
  • 2,452
  • 10
  • 15
1

Use the XMLSerializer

For e.g. Serialize like this:

    private async Task SaveSettings(Settings settings)
    {

        var folder = Windows.Storage.ApplicationData.Current.LocalFolder;
        var options = Windows.Storage.CreationCollisionOption.ReplaceExisting;

        var file = await folder.CreateFileAsync("Settings.XML", options);
        try
        {


            XmlSerializer SerializerObj = new XmlSerializer(typeof(Settings));

            SerializerObj.Serialize(await file.OpenStreamForWriteAsync(), settings);
        }
        catch
        {
            // handle any kind of exceptions
        }

    }

Deserialize like this:

    private async Task<Settings> LoadSettings()
    {
        Settings settings = new Settings(); 
        var folder = Windows.Storage.ApplicationData.Current.LocalFolder;
        try
        {
            var file = await folder.GetFileAsync("Settings.XML");
            XmlSerializer SerializerObj = new XmlSerializer(typeof(Settings));
            settings = SerializerObj.Deserialize(await file.OpenStreamForReadAsync()) as Settings;
        }
        catch (Exception ex)
        {
            // handle any kind of exceptions
        }
        return settings;
    }

This example serializes an object called Settings. You can change it to serialize your array of objects.

This is from a windows 8 app so you may need to adapt it slightly.

AndrewJE
  • 858
  • 2
  • 9
  • 19