0
votes

I'm working on a project where I have this list

List<Vara> minaVaror = new List<Vara>();

It's created from this class:

class Vara
{
    public double streckKod { get; set; }
    public string artNamn { get; set; }
}

And this is how I add items to the list:

minaVaror.Add(new Vara() {streckKod = inputBox1, artNamn = textBox2.Text });

Alright so this list is going to be added items to every now and then so I need to be able to save and load the content/items of the list so you won't lose the data when the program is closed and reopened.

We did something like this in class last year where we saved the data over to an XML file with XmlSerializer however that was only for 1 int, I'm not really sure how to do it for a whole list.

1

1 Answers

0
votes

By definition, XmlSerializer cannot deserialize a List<T> or an ArrayList. From MSDN.

The XmlSerializer cannot deserialize the following: arrays of ArrayList and arrays of List<T>.

So you can serialize a List<T>, but you cannot deserialize List<T>.

So you can use this code to serialize in XML and deserialize an XML file.

namespace DataContractSerializerExample
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Xml;

    // You must apply a DataContractAttribute or SerializableAttribute
    // to a class to have it serialized by the DataContractSerializer.
    [DataContract(Name = "Vara", Namespace = "http://www.contoso.com")]
    public class Vara
    {
        [DataMember()]
        public double streckKod { get; set; }
        [DataMember]
        public string artNamn { get; set; }
    }

    public sealed class Test
    {
        private Test() { }

        public static void Main()
        {
            List<Vara> minaVaror = new List<Vara>() { new Vara() { streckKod = 5.0, artNamn = "test1" }, new Vara() { streckKod = 5.0, artNamn = "test2" }, new Vara() { streckKod = 5.0, artNamn = "test3" } };
            string fileName = "test.xml";
            Serialize<List<Vara>>(fileName, minaVaror);
            List<Vara> listDes = Deserialize<List<Vara>>(fileName);
        }

        public static void Serialize<T>(string fileName, T obj)
        {
            FileStream writer = new FileStream(fileName, FileMode.Create);
            DataContractSerializer ser =
                new DataContractSerializer(typeof(T));
            ser.WriteObject(writer, obj);
            writer.Close();
        }

        public static T Deserialize<T>(string fileName)
        {
            FileStream fs = new FileStream(fileName,
            FileMode.Open);
            XmlDictionaryReader reader =
                XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());
            DataContractSerializer ser = new DataContractSerializer(typeof(T));
            T  des  =
                (T)ser.ReadObject(reader, true);
            reader.Close();
            fs.Close();
            return des;
        }
    }
}

Note: You should add a reference to the C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\System.Runtime.Serialization.dll.