0

我在将列表项保存到独立存储时遇到问题。(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) 中存在错误。

如果您能帮助我,我将不胜感激。提前致谢

4

1 回答 1

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);
  }
}
于 2013-04-24T22:38:43.050 回答