xml 序列化/反序列化有问题

本文关键字:有问题 反序列化 序列化 xml | 更新日期: 2023-09-27 18:35:58

我正在涉足Windows Phone开发,并开始接受Silverlight在WP上的一些功能,但我正在努力解决XML问题:

我正在尝试将一些对象序列化为 XML,然后读取所述 XML 并再次将其序列化为对象。然后,我将使用它来填充列表框,方法是将ObservableCollection作为列表框的ItemsSource

我已经确保数据绑定正常工作;如果我只是生成对象并将它们放入Observable Collection,然后将其作为ItemsSource,则没有问题。似乎我的代码的XML部分出错了。一切都编译和执行得足够好,但列表框仍然是空的:(

此代码在应用程序启动时执行(不是很有效,但适用于我的测试):

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        ObservableCollection<Quote> quotes = new ObservableCollection<Quote>();
        for (int i = 0; i < 10; i++)
        {
            Quote quote = new Quote()
            {
                Author = "Author #" + i.ToString(),
                QuoteText = "This is quote #" + i.ToString(),
            };
            quotes.Add(quote);
        }
        XmlWriterSettings xmlwrtrstngs = new XmlWriterSettings();
        xmlwrtrstngs.Indent = true;
        using(IsolatedStorageFile isostrg = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using(IsolatedStorageFileStream isoflstrm = isostrg.OpenFile("Quotes.xml", FileMode.Create))
            {
                XmlSerializer xmlsrlzr = new XmlSerializer(typeof(QuoteCollection));
                using(XmlWriter xmlwrtr = XmlWriter.Create(isoflstrm, xmlwrtrstngs))
                {
                    xmlsrlzr.Serialize(xmlwrtr, quoteCollection);
                }
            }
        }
        loadData();
    }
    void loadData()
    {
        try
        {
            using(IsolatedStorageFile isostrg = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using(IsolatedStorageFileStream isoflstrm = isostrg.OpenFile("Quotes.xml", FileMode.Open))
                {
                    XmlSerializer xmlsrlzr = new XmlSerializer(typeof(QuoteCollection));
                    QuoteCollection quoteCollectionFromXML = (QuoteCollection)xmlsrlzr.Deserialize(isoflstrm);
                    LstBx.ItemsSource = quoteCollectionFromXML.Quotes;
                }
            }
        }
        catch(Exception)
        {
            Console.Write("Something went wrong with the XML!");
        }

    }

报价收藏

public class QuoteCollection : INotifyPropertyChanged
{
    ObservableCollection<Quote> quotes;
    public event PropertyChangedEventHandler PropertyChanged;
    public ObservableCollection<Quote> Quotes
    {
        get { return quotes; }
        set
        {
            if(quotes != value)
            {
                quotes = value;
                raisePropertyChanged("Quotes");
            }
        }
    }
    protected virtual void raisePropertyChanged(string argPropertyChanged)
    {
        if(PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(argPropertyChanged));
        }
    }
}

报价

public class Quote : INotifyPropertyChanged
{
    string author;
    string quoteText;
    public event PropertyChangedEventHandler PropertyChanged;
    public string Author
    {
        get
        {
            return author;
        }
        set
        {
            if(author != value)
            {
                author = value;
                onPropertyChanged("Author");
            }
        }
    }
    public string QuoteText
    {
        get
        {
            return quoteText;
        }
        set
        {
            if(quoteText != value)
            {
                quoteText = value;
                onPropertyChanged("QuoteText");
            }
        }
    }
    protected virtual void onPropertyChanged(string argProperty)
    {
        if(PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(argProperty));
        }
    }
}

任何见解将不胜感激:)

xml 序列化/反序列化有问题

您正在使用QuoteCollection作为类型进行序列化,但实际上写出了一个ObservableCollection<Quote>

忘了将 ObservableCollection 放入我之前创建的 QuoteCollection 实例中(未显示在发布的代码中)。