Advertisement
Guest User

Saving Date and Names to IsolatedStorage

a guest
Feb 15th, 2012
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.28 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Animation;
  11. using System.Windows.Shapes;
  12. using Microsoft.Phone.Controls;
  13. using System.Xml.Linq;
  14. using System.Collections.ObjectModel;
  15. using System.IO.IsolatedStorage;
  16. using System.IO;
  17. using System.Xml;
  18. using System.Xml.Serialization;
  19.  
  20. namespace LasnaoloAppsi
  21. {
  22.     public partial class OppilasLista : PhoneApplicationPage
  23.     {
  24.         XDocument lista = XDocument.Load("NykyisetKurssit.xml");
  25.         XDocument oppilasInfo = XDocument.Load("Oppilaat.xml");
  26.         private ObservableCollection<Kurssit> _Kurssit = new ObservableCollection<Kurssit>();
  27.         List<Kurssit> selected = new List<Kurssit>();
  28.         string id = string.Empty;
  29.  
  30.         public OppilasLista()
  31.         {
  32.             InitializeComponent();
  33.            
  34.         }
  35.  
  36.         protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
  37.         {
  38.             if (NavigationContext.QueryString.TryGetValue("id", out id))
  39.             {
  40.  
  41.                 var ryhma = (from ryhmaInfo in lista.Descendants("Kurssi")
  42.                              where ryhmaInfo.Attribute("id").Value == id
  43.                              select new Kurssit
  44.                              {
  45.                                  RyhmanNimi = (string)ryhmaInfo.Element("tunnus").Value
  46.  
  47.                              }).FirstOrDefault();
  48.  
  49.                 PageTitle.Text = ryhma.RyhmanNimi;
  50.  
  51.                 var oppilas = (from oppilaat in oppilasInfo.Descendants("Oppilas")
  52.                             where oppilaat.Attribute("ryhma").Value == id
  53.                             select new Kurssit
  54.                             {
  55.                                 OppilaanNimi = (string)oppilaat.Element("nimi").Value
  56.                             });
  57.  
  58.                 foreach (var item in oppilas)
  59.                     _Kurssit.Add(item);
  60.  
  61.                 OppilaidenLista.ItemsSource = _Kurssit;
  62.                
  63.             }
  64.  
  65.             base.OnNavigatedTo(e);
  66.         }
  67.  
  68.         private void Tallenna_Button_Click(object sender, RoutedEventArgs e)
  69.         {
  70.             selected = _Kurssit.Where(x => x.IsChecked == true).ToList();
  71.            
  72.             const string id1 = "504T11";
  73.             const string id2 = "504T12";
  74.             const string id3 = "504T10";
  75.             const string id4 = "504T09";
  76.  
  77.             string r504T11 = "People.xml";
  78.             string r504T12 = "People2.xml";
  79.             string r504T10 = "People3.xml";
  80.             string r504T09 = "People4.xml";
  81.  
  82.             string d504T11 = "CurrentDate504T110";
  83.             string d504T12 = "CurrentDate504T120";
  84.             string d504T10 = "CurrentDate504T100";
  85.             string d504T09 = "CurrentDate504T090";
  86.  
  87.             switch (id)
  88.             {
  89.                 case id1:
  90.  
  91.                     saveLasnaolot(r504T11);
  92.                     saveLasnaoloDate(d504T11);
  93.                     break;
  94.  
  95.                 case id2:
  96.  
  97.                     saveLasnaolot(r504T12);
  98.                     saveLasnaoloDate(d504T12);
  99.                     break;
  100.  
  101.                 case id3:
  102.                     saveLasnaolot(r504T10);
  103.                     saveLasnaoloDate(d504T10);
  104.                     break;
  105.  
  106.                 case id4:
  107.                     saveLasnaolot(r504T09);
  108.                     saveLasnaoloDate(d504T09);
  109.                     break;
  110.             }
  111.                
  112.         }
  113.  
  114.         public void saveLasnaoloDate(string currentDate)
  115.         {
  116.             int i = 1;
  117.             var settings = IsolatedStorageSettings.ApplicationSettings;
  118.  
  119.             Kurssit date = new Kurssit()
  120.             {
  121.                 Aika = DateTime.Now.ToString("dd/MM/yyyy") + string.Format(" {0}", i.ToString())
  122.             };
  123.  
  124.             if (settings.Contains(currentDate))
  125.             {
  126.                 string findFileNumber = currentDate.Substring(17, 1);
  127.                 int foundNum = int.Parse(findFileNumber);
  128.                 int newNum = foundNum++;
  129.                 string newFilename = currentDate + newNum.ToString();
  130.                 settings[newFilename] = date;
  131.             }
  132.             else
  133.                 settings.Add(currentDate, date);
  134.  
  135.             settings.Save();
  136.         }
  137.  
  138.         public void saveLasnaolot(string fileName)
  139.         {
  140.             // Write to the Isolated Storage
  141.             XmlWriterSettings x_W_Settings = new XmlWriterSettings();
  142.             x_W_Settings.Indent = true;
  143.             using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
  144.             {
  145.                 using (IsolatedStorageFileStream stream = ISF.OpenFile(fileName, FileMode.Create))
  146.                 {
  147.                     XmlSerializer serializer = new XmlSerializer(typeof(List<Kurssit>));
  148.                     using (XmlWriter xmlWriter = XmlWriter.Create(stream, x_W_Settings))
  149.  
  150.                         serializer.Serialize(xmlWriter, selected);
  151.                     MessageBox.Show("Läsnäolot Tallennettu Arkistoon!!!! " + DateTime.Now.ToString("dd/MM/yyyy"));
  152.  
  153.                    
  154.                 }
  155.                 }
  156.             }
  157.         }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement