1
votes

I have been trying to serialize a list of objects from a class and keep getting an error stating there is an error in the XML file at point (25, 6)(these numbers change depending on what I am trying to serialize).

Here's an example of how I am trying to serialize the data:

using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
    using((IsolatedStorageFileStream fs = isf.CreateFile("data.dat"))
    {
        XmlSerializer ser = new XmlSerializer(User.Data.GetType());
        ser.Serialize(fs, User.Data);
} }

And here's how I am deserializing the data:
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
    if (isf.FileExists("Data.dat"))
    {
        using (IsolatedStorageFileStream fs = isf.OpenFile("Data.dat", System.IO.FileMode.Open))
        {
            XmlSerializer ser = new XmlSerializer(User.Data.GetType());
            object obj = ser.Deserialize(fs);
            if (null != obj && obj is Data)
                User.Data= obj as Data;
        }
    }
}

I don't see any initial problems with this portion of the code, but it crashes on every list of objects I pass it.

Here's a sample of the class I'm using:

public class Data
    {
        public static int counter;

        public Data() { this.index = counter++; }

        public DateTime availablefrom { get; set; }
        public DateTime availableuntil { get; set; }
        public string course { get; set; }
        public DateTime? datetaken { get; set; }
        public double duration { get; set; }
        public string instructions { get; set; }
        public string instructorname { get; set; }
        public double scorepointscorrect { get; set; }
        public double scorepointspossible { get; set; }
        public string testname { get; set; }
        public int index { get; private set; }
    }

When I give the serializer just simple classes it works, so I know the serializer itself is working, but when I create a list of objects from my Data class or other classes, it crashes. Anyone have any suggestions?

3

3 Answers

2
votes

Since "index" is a public property of data, the deserializer is trying to set the value of it. This fails because set for "index" is private. Try setting "index" to internal instead of public and it should deserialize correctly.

1
votes

Try passing in the list type instead of the type that s in the list. For example:

XmlSerializer serializer = new XmlSerializer(typeof(List<Incident>));

instead of

XmlSerializer serializer = new XmlSerializer(typeof(Incident));

0
votes

Here is how I save and load lists of objects to and from Isolated Storage:

    private static IsolatedStorageFile _isoStore;
    public static IsolatedStorageFile IsoStore 
    { 
        get { return _isoStore ?? (_isoStore = IsolatedStorageFile.GetUserStoreForApplication()); }
    }

    public static void SaveList<T>(string folderName, string dataName, ObservableCollection<T> dataList) where T : class
    {
        if (!IsoStore.DirectoryExists(folderName))
        {
            IsoStore.CreateDirectory(folderName);
        }

        string fileStreamName = string.Format("{0}\\{1}.dat", folderName, dataName);

        using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(fileStreamName, FileMode.Create, IsoStore))
        {
            DataContractSerializer dcs = new DataContractSerializer(typeof(ObservableCollection<T>));
            dcs.WriteObject(stream, dataList);
        }
    }

    public static ObservableCollection<T> LoadList<T>(string folderName, string dataName) where T : class
    {
        ObservableCollection<T> retval = new ObservableCollection<T>();

        string fileStreamName = string.Format("{0}\\{1}.dat", folderName, dataName);

        using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(fileStreamName, FileMode.OpenOrCreate, IsoStore))
        {
            if (stream.Length > 0)
            {
                DataContractSerializer dcs = new DataContractSerializer(typeof(ObservableCollection<T>));
                retval = dcs.ReadObject(stream) as ObservableCollection<T>;
            }
        }

        return retval;
    }