Something wrong with xml serialization/deserialization

scottyboydfc

New Member
I'm dipping my feet into Windows Phone development and are starting to get to terms with some of the features of Silverlight on WP, but I'm struggling with XML:I'm trying to serialize some objects into an XML and then read the said XML and serialize it into objects again. Then I'll use that to populate a listbox by putting an \[code\]ObservableCollection\[/code\] as the \[code\]ItemsSource\[/code\] of the listbox.I've already made sure that the databinding works properly; if I just generate the objects and put them into an \[code\]Observable Collection\[/code\] and then put that as the \[code\]ItemsSource\[/code\], there are no problems. It would seem that it's the XML part of my code that's faulting. Everything compiles and executes nice enough, but the listbox remains empty :( This code executes as the app launches (not very effective but works for my testing):\[code\]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!"); } }\[/code\]QuoteCollection\[code\]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)); } }}\[/code\]Quote\[code\]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)); } }}\[/code\]Any insight would be most appreciated :)
 
Back
Top