1

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 Answers 3

2

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.

2
  • I was really hoping this suggestion would work because it hadn't dawned on me that I had set it to private. However, changing it to internal still caused the same error and even changing it to public set caused the same error. Here's what it says specifically, just for clarification: An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll Additional information: There is an error in XML document (2, 2).
    – legacybass
    Jan 26, 2011 at 17:40
  • I just realized that, in an attempt to find a work around, I had made a change last night after posting this help request that was telling the serializer different information than what I was trying to deserialize. After changing the "index" property to internal it works perfectly. You guys are amazing!
    – legacybass
    Jan 26, 2011 at 17:48
1

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));

1
  • I have actually tried it both ways several times. I've even tried a combination of the two in the serializing steps and the deserializing steps, but to no avail. I probably should have made it more clear in my code examples, but User.Data is actually a List<Data> object. I just changed the name of the class and object to make it more generic.
    – legacybass
    Jan 26, 2011 at 17:37
0

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;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.