ilerleartik
ilerleartik

Reputation: 115

Saving my list to Isolated Storage

I'm having an issue while saving my list items to isolated storage.(Windows Phone 7)

Here is how I defined my list class; I'm using data binding

public class CTransaction
    {
        public String Date1 { get; set; }
        public String Amount1 { get; set; }
        public String Type1 { get; set; }
        public String Time1 { get; set; }
        public String Dis1 { get; set; }
        public String Def1 { get; set; }
        public CTransaction(String date1, String amount1, String type1, String time1, String dis1, String def1)
        {
            this.Date1 = date1;
            this.Amount1 = amount1;
            this.Time1 = time1;
            this.Dis1 = dis1;
            this.Def1 = def1;
            switch (type1)
            {
                case "FR":
                    this.Type1 = "Images/a.png";
                    break;

                case "TA":
                    this.Type1 = "Images/b.png";
                    break;

                case "DA":
                    this.Type1 = "Images/c.png";
                    break;

                case "CC":
                    this.Type1 = "Images/mount.png";
                    break;
            }
        }
    }

Here is how I insert the items to my list; I'm assigning something to the fields and then I'm inserting this into my list like;

List<CTransaction> ctransactionList = new List<CTransaction>();
//some list item filling operations here***

ctransactionList.Insert(0, new CTransaction(DefaultDate, DefaultAmount, RandomType, Times, Dist, Defin));//This one inserts at the top of my list

        CTransactionList.ItemsSource = null;
        CTransactionList.ItemsSource = ctransactionList; //These two operations refreshes my list

!!Yes, you see how I insert and create my list. Everything is OK. now I want to save ctransactionList

Here is How I save it;(by clicking a button)

  var store = IsolatedStorageFile.GetUserStoreForApplication();
        if (store.FileExists("saver"))
        {
            store.DeleteFile("saver");
        }
        using (var stream = new IsolatedStorageFileStream("saver", FileMode.OpenOrCreate, FileAccess.Write, store))
        {
            var serializer = new XmlSerializer(typeof(List<CTransaction>));
            serializer.Serialize(stream, ctransactionList);
        }

Here is How I load it (By clicking another button)

var store = IsolatedStorageFile.GetUserStoreForApplication();

        if (store.FileExists("saver"))
        {
            using (var stream = new IsolatedStorageFileStream("saver", FileMode.OpenOrCreate, FileAccess.Read, store))
            {
                var reader = new StreamReader(stream);

                if (!reader.EndOfStream)
                {
                    var serializer = new XmlSerializer(typeof(List<CTransaction>));
                    ctransactionList = (List<CTransaction>)serializer.Deserialize(reader);
                }
            }
        }

While debugging, saving operation seems consistent and not raising any error, but while loading; this is the raised error at this line;

ctransactionList = (List<CTransaction>)serializer.Deserialize(reader);

ERROR: There is an error in XML document (3, 4).

If you can help me, I'd really appreciate it. Thanks in advance

Upvotes: 0

Views: 809

Answers (1)

steFranceschi
steFranceschi

Reputation: 93

Try this to make your class serializable:

[DataContract]
    public class CTransaction
    {
        [DataMember]
        public String Date1 { get; set; }
        [DataMember]
        public String Amount1 { get; set; }
        [DataMember]
        public String Type1 { get; set; }
        [DataMember]
        public String Time1 { get; set; }
        [DataMember]
        public String Dis1 { get; set; }
        [DataMember]
        public String Def1 { get; set; }
        public CTransaction(String date1, String amount1, String type1, String time1, String dis1, String def1)
        {
          //your code
        }
    }

then change the load and save methods to:

public List<CTransaction> LoadFromIsolatedStorage()
{
  List<CTransaction> ret = new List<CTransaction>();
  try
  {
    var store = IsolatedStorageFile.GetUserStoreForApplication();
    if (store.FileExists("saver"))
    {
      using (var stream = new IsolatedStorageFileStream("saver", FileMode.OpenOrCreate, FileAccess.Read, store))
      {
        if (stream.Length > 0)
        {
           DataContractSerializer dcs = new DataContractSerializer(typeof(List<CTransaction>));
           ret = dcs.ReadObject(stream) as List<CTransaction>;
        }
      }
    }
  }
  catch (Exception ex)
  {
    // handle exception
  }
}

public void SaveToIsolatedStorage(List<CTransaction> listToSave)
{
  var store = IsolatedStorageFile.GetUserStoreForApplication();
  if (store.FileExists("saver"))
  {
     store.DeleteFile("saver");
  }
  using (var stream = new IsolatedStorageFileStream("saver", FileMode.OpenOrCreate, FileAccess.Write, store))
  {
    DataContractSerializer dcs = new DataContractSerializer(typeof(List<CTransaction>));
   dcs.WriteObject(stream, ctransactionList);
  }
}

Upvotes: 2

Related Questions