简体   繁体   中英

Windows Phone 7 Deserialize Exception

I am trying to read in an XML file that I have saved to an IsolatedStorageFile using Serialize. When I try to read back the XML file in a try block it throws an exception at the Deserialization step. Am I doing something wrong? How can I fix this?

Bet Class:

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

Save/Load Bet functions

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

Thank you!

What is the exception?

As I can remeber you need a constructor without parameters for this...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM