Windows Phone 7 反序列化异常
本文关键字:异常 反序列化 Phone Windows | 更新日期: 2023-09-27 18:36:56
>我正在尝试读取已使用序列化保存到隔离存储文件的XML文件。当我尝试在 try 块中读回 XML 文件时,它会在反序列化步骤中引发异常。我做错了什么吗?我该如何解决这个问题?
投注类别:
public class Bet
{
public String Amount { get; set; }
public String Opponent { get; set; }
public String Terms { get; set; }
public int Result { get; set; }
public String ResultColor { get; set; }
public Bet(String amount, String opponent, String terms, int result, String rcolor)
{
this.Amount = amount;
this.Opponent = opponent;
this.Terms = terms;
this.Result = result;
this.ResultColor = rcolor;
}
}
保存/加载投注功能
public void SaveBets()
{
List<Bet> bets = new List<Bet>();
foreach (Bet item in openBetList)
bets.Add(item);
foreach (Bet item in closedBetList)
bets.Add(item);
// Write to the Isolated Storage
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Indent = true;
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Bets.xml", FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Bet>));
using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
{
serializer.Serialize(xmlWriter, bets);
}
}
}
}
public void LoadBets()
{
try
{
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Bets.xml", FileMode.Open))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Bet>));
List<Bet> data = (List<Bet>)serializer.Deserialize(stream);
if(data.Count > 0)
foreach (Bet item in data)
{
if (item.Result == 0)
openBetList.Add(item);
else
closedBetList.Add(item);
}
}
}
}
catch
{
//add some code here
}
}
谢谢!
有什么例外?
我记得你需要一个没有参数的构造函数......