Saving and opening custom files using Serialization in c#

1k views Asked by At

Three-part question.

I posted a question yesterday asking about nested classes and such (great help, by the way). However, now I am continuing with this same program, which utilizes an MDI to keep track of multiple generic stores and their respective inventories, to be instantiated whenever opened from a custom .inv file. To this end, both the Store class and Record class are serializable to save to said custom file.

I don't know what it is, but I just don't understand Serialization. I understand the concept well enough, just not the practice, so I ask for a bit of patience.

Part 1) I've created a saveFileDialog in my original form, designed to save whatever files I put into it with a .inv file extension. Now, the question is how exactly do I pass it an instantiated class to save in such a way that allows me to open the file (below) later?

Part 2) For the openFileDialog, two parts.

Firstly, after saving it above, how do I read the information in? I mean, I thought about using a text document with a renamed file extension to store all values just so I could read in the store name and the records one at a time, but since I don't know serialization as well as I need to, I don't know if that's a good idea or not.

And secondly, how would I go about instantiating the instance I'm trying to open? Is it as simple as creating one instance of the class and every time I open a new document I clear everything and just add the values from the opened file, or is it more complex than that?

The code for the classes is below, since this'll give you an idea at what I'm intending to save. If you need any other code, let me know.

using System.Text;
using System.Threading.Tasks;

namespace Inventory
{
    [Serializable]
    class Store
    {
        public Store() { }
        public Store(string name) { }
        public string name { get; set; }

        public List<Record> Records { get; private set; }
    }

    [Serializable]
    class Record
    {
        public Record() { }
        public Record(int ID, int Quantity, double Price, string Name) { }
        public int id { get; set; }
        public int quantity { get; set; }
        public double price { get; set; }
        public string name { get; set; }
    }
}
2

There are 2 answers

0
Roberto Conte Rosito On

You can use a simple helper to serialize/deserialize strings (I've built one for you as an example):

public static class StoreHelper
{
    public static string Serialize(Store store)
    {
        var xmlSerializer = new XmlSerializer(typeof(Store));
        var textWriter = new StringWriter();

        xmlSerializer.Serialize(textWriter, store);
        return textWriter.ToString();
    }

    public static Store Deserialize(string xml)
    {
        var serializer = new XmlSerializer(typeof(Store));
        Store result;

        using (TextReader reader = new StringReader(xml))
        {
            result = (Store)serializer.Deserialize(reader);
        }

        return result;
    }
}

Now in you MDI app you can manage store XML using the helper in this way:

// Generate Store XML from Object.
var storeXml = StoreHelper.Serialize(myStore);
// Generate Store Object from XML.
var store = StoreHelper.Deserialize(storeXml);

Now you can read/write xml from your file and handle it. I hope this can help.

0
Jason White On

Let .NET do it for you:

private void Serialize(Store store, string path)
{
    var xmlSerializer = new XmlSerializer(typeof(Store));

    using (var streamWriter = new StreamWriter(path))
    {
        xmlSerializer.Serialize(streamWriter, store);
    }
}

private Store Deserialize(string path)
{
    var xmlSerializer = new XmlSerializer(typeof(Store));

    using (var streamReader = new StreamReader(path))
    {
        return xmlSerializer.Deserialize(streamReader) as Store;
    }
}