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?
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.