将我的列表保存到隔离存储

时间:2013-04-24 20:54:42

标签: list windows-phone-7 isolatedstorage

将列表项保存到隔离存储时遇到问题。(Windows Phone 7)

以下是我定义列表类的方法;我正在使用数据绑定

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

以下是我将项目插入列表的方法;我正在为字段分配内容,然后我将其插入到我的列表中,如

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

!!是的,你看我如何插入和创建我的列表。一切都好。现在我要保存 ctransactionList

以下是我如何保存;(点击按钮)

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

以下是我如何加载它(点击另一个按钮)

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

在调试时,保存操作似乎一致并且不会引发任何错误,但在加载时;这是此行引发的错误;

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

错误:XML文档中存在错误(3,4)。

如果你能帮助我,我真的很感激。 提前致谢

1 个答案:

答案 0 :(得分:2)

尝试使这个类可序列化:

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

然后更改负载并将方法保存到:

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