簡體   English   中英

Windows Phone 7反序列化異常

[英]Windows Phone 7 Deserialize Exception

我正在嘗試讀取使用Serialize保存到IsolatedStorageFile的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
    }
}

謝謝!

有什么例外?

據我所知,您需要一個沒有參數的構造函數...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM