public class CommitteeDataModel { public CommitteeDataModel() { } public static ObservableCollection<Committee> GetCommittees() { ObservableCollection<Committee> committee = new ObservableCollection<Committee>(); committee.Add(new Committee() { Name = "Committee I" }); committee.Add(new Committee() { Name = "Committee II" }); committee.Add(new Committee() { Name = "Committee III" }); return committee; } } public class Committee { public string Name { get; set; } public ObservableCollection<CommitteeAgenda> Agendas { get; set; } public Committee() { Agendas = new ObservableCollection<CommitteeAgenda>(); Agendas.Add(new CommitteeAgenda() { Name = "CommitteeAgenda I" }); Agendas.Add(new CommitteeAgenda() { Name = "CommitteeAgenda II" }); Agendas.Add(new CommitteeAgenda() { Name = "CommitteeAgenda III" }); } } public class CommitteeAgenda { public string Name { get; set; } public ObservableCollection<CommitteeAgendaItem> CommitteeAgendaItems { get; set; } public ObservableCollection<CommitteeAgendaDocument> CommitteeAgendaDocuments { get; set; } public CommitteeAgenda() { CommitteeAgendaItems = new ObservableCollection<CommitteeAgendaItem>(); CommitteeAgendaItems.Add(new CommitteeAgendaItem() { Name = "CommitteeAgendaItem I" }); CommitteeAgendaItems.Add(new CommitteeAgendaItem() { Name = "CommitteeAgendaItem II" }); CommitteeAgendaItems.Add(new CommitteeAgendaItem() { Name = "CommitteeAgendaItem III" }); CommitteeAgendaDocuments = new ObservableCollection<CommitteeAgendaDocument>(); CommitteeAgendaDocuments.Add(new CommitteeAgendaDocument() { Name = "CommitteeAgendaDocument I" }); CommitteeAgendaDocuments.Add(new CommitteeAgendaDocument() { Name = "CommitteeAgendaDocument II" }); CommitteeAgendaDocuments.Add(new CommitteeAgendaDocument() { Name = "CommitteeAgendaDocument III" }); } } public class CommitteeAgendaItem { public string Name { get; set; } public CommitteeAgendaItem() { } } public class CommitteeAgendaDocument { public string Name { get; set; } public CommitteeAgendaDocument() { } }
async Task writeXmlAsync() { IStorageFile xmlFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("Committees.xml", CreationCollisionOption.ReplaceExisting); XmlSerializer xmlSerializer = new XmlSerializer(typeof(ObservableCollection<Committee>)); MemoryStream memoryStream = new MemoryStream(); ObservableCollection<Committee> committees = CommitteeDataModel.GetCommittees(); xmlSerializer.Serialize(memoryStream, committees); memoryStream.Seek(0, SeekOrigin.Begin); string serialized = new StreamReader(memoryStream).ReadToEnd(); memoryStream.Dispose(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(serialized); await xmlDoc.SaveToFileAsync(xmlFile); }
async Task<ObservableCollection<Committee>> readXmlAsync() { ObservableCollection<Committee> committeesRead = null; IStorageFile xmlReadFile = await ApplicationData.Current.LocalFolder.GetFileAsync("Committees.xml"); using (Stream stream = await xmlReadFile.OpenStreamForReadAsync()) { XmlSerializer xmlReadSerializer = new XmlSerializer(typeof(ObservableCollection<Committee>)); committeesRead = xmlReadSerializer.Deserialize(stream) as ObservableCollection<Committee>; } return committeesRead; }
<ListView ItemsSource="{Binding Committees}"> <ListView.ItemTemplate> <DataTemplate> <Grid Width="600" Height="30"> <TextBlock Text="{Binding Name}" Width="120" Height="30" /> </Grid> </DataTemplate> </ListView.ItemTemplate> </ListView>
protected override async void LoadState(Object navigationParameter, Dictionary<String, Object> pageState) { this.DefaultViewModel["Committees"] = await readXmlAsync(); }
ObservableCollection<Committee> lstCommittees = this.DefaultViewModel["Committees"] as ObservableCollection<Committee>; if (lstCommittees != null) { foreach (Committee committee in lstCommittees) { string committeeName = committee.Name; foreach (CommitteeAgenda committeeAgenda in committee.Agendas) { string committeeAgendaIdentifier = committeeAgenda.Name; } } }