Advertisement
Guest User

Untitled

a guest
Jul 15th, 2017
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices.WindowsRuntime;
  6. using Windows.Foundation;
  7. using Windows.Foundation.Collections;
  8. using Windows.UI.Xaml;
  9. using Windows.UI.Xaml.Controls;
  10. using Windows.UI.Xaml.Controls.Primitives;
  11. using Windows.UI.Xaml.Data;
  12. using Windows.UI.Xaml.Input;
  13. using Windows.UI.Xaml.Media;
  14. using Windows.UI.Xaml.Navigation;
  15. using Windows.UI.Xaml;
  16. using Windows.UI.Xaml.Controls;
  17. using System.Xml;
  18. using System.Xml.Linq;
  19. using Windows.Storage;
  20. using System.Collections.ObjectModel;
  21. using System.Reflection;
  22. using System.Diagnostics;
  23. using Windows.ApplicationModel;
  24. using Windows.Storage.Streams;
  25.  
  26. // The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
  27.  
  28. namespace ContactsApp
  29. {
  30.     public sealed partial class MainPage : Page
  31.     {
  32.  
  33.         //string XMLPath = Path.Combine(Package.Current.InstalledLocation.Path, "Contacts.xml");
  34.         private ObservableCollection<string> lstd = new ObservableCollection<string>();
  35.         public ObservableCollection<string> myList { get { return lstd; } }
  36.         public MainPage()
  37.         {
  38.             this.InitializeComponent();
  39.         }
  40.  
  41.         private async void Grid_Loading(FrameworkElement sender, object args)
  42.         {
  43.             loadContacts();
  44.         }
  45.         public async void loadContacts()
  46.         {
  47.  
  48.             StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("Contacts.xml");
  49.             XmlReader xmlReader = XmlReader.Create(file.Path);
  50.             while (xmlReader.Read())
  51.             {
  52.                 if (xmlReader.Name.Equals("ID") && (xmlReader.NodeType == XmlNodeType.Element))
  53.                 {
  54.                     lstd.Add(xmlReader.ReadElementContentAsString());
  55.                 }
  56.             }
  57.             DataContext = this;
  58.             xmlReader.Dispose();
  59.         }
  60.  
  61.         private async void lstBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  62.         {
  63.             StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("Contacts.xml");
  64.             XDocument xml = XDocument.Load(file.Path);
  65.             if (lstBox.SelectedIndex != -1)
  66.             {
  67.                 var nodes = (from n in xml.Descendants("Contact").
  68.             Where(r => r.Element("ID").Value == lstBox.SelectedItem.ToString())
  69.                              select new
  70.                              {
  71.                                  ID = (string)n.Element("ID").Value,
  72.                                  FirstName = (string)n.Element("FirstName").Value,
  73.                                  LastName = (string)n.Element("LastName").Value,
  74.                                  Mobile = (string)n.Element("Mobile").Value,
  75.                                  Email = (string)n.Element("Email").Value
  76.                              });
  77.                 foreach (var n in nodes)
  78.                 {
  79.                     txtID.Text = n.ID;
  80.                     txtFirstName.Text = n.FirstName;
  81.                     txtLastName.Text = n.LastName;
  82.                     txtMobile.Text = n.Mobile;
  83.                     txtEmail.Text = n.Email;
  84.                 };
  85.             }
  86.             else
  87.             {
  88.                 txtID.Text = "";
  89.                 txtFirstName.Text = "";
  90.                 txtLastName.Text = "";
  91.                 txtMobile.Text = "";
  92.                 txtEmail.Text = "";
  93.             }
  94.            
  95.         }
  96.  
  97.         private async void updateXMLFile(XDocument xdoc)
  98.         {
  99.             StorageFolder installedLocation = ApplicationData.Current.LocalFolder;
  100.             try
  101.             {
  102.                 StorageFile file = await installedLocation.CreateFileAsync("Contacts.xml", CreationCollisionOption.ReplaceExisting);
  103.                 await FileIO.WriteTextAsync(file, xdoc.ToString());
  104.             }
  105.             catch (Exception ex)
  106.             {
  107.                 String s = ex.ToString();
  108.             }
  109.         }
  110.  
  111.         private async void btnUpdateContact_Click(object sender, RoutedEventArgs e)
  112.         {
  113.             StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("Contacts.xml");
  114.             XDocument xdoc = XDocument.Load(file.Path);
  115.             if (lstBox.SelectedIndex != -1)
  116.             {
  117.                 XElement upd = (from elements in xdoc.Descendants("Contact")
  118.                                 where elements.Element("ID").Value == lstBox.SelectedItem.ToString()
  119.                                 select elements).Single();
  120.                 upd.Element("ID").Value = txtID.Text;
  121.                 upd.Element("FirstName").Value = txtFirstName.Text;
  122.                 upd.Element("LastName").Value = txtLastName.Text;
  123.                 upd.Element("Mobile").Value = txtMobile.Text;
  124.                 upd.Element("Email").Value = txtEmail.Text;
  125.                 updateXMLFile(xdoc);
  126.             }
  127.         }
  128.  
  129.  
  130.         private async void btnDeleteContact_Click(object sender, RoutedEventArgs e)
  131.         {
  132.             StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("Contacts.xml");
  133.             XDocument xdoc = XDocument.Load(file.Path);
  134.             if (lstBox.SelectedIndex != -1)
  135.             {
  136.                 xdoc.Element("Contacts")
  137.                     .Elements("Contact")
  138.                     .Where(x => (string)x.Attribute("ID") == lstBox.SelectedItem.ToString()).Remove();
  139.                 lstBox.SelectedIndex = -1;
  140.                 updateXMLFile(xdoc);
  141.             }
  142.         }
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement