Click here to Skip to main content
15,886,422 members
Articles / Desktop Programming / WPF

An Address Book Application Made in MVVM For Metro Style App in Windows 8 Part 2

Rate me:
Please Sign up or sign in to vote.
4.40/5 (5 votes)
29 May 2012CPOL6 min read 41K   1.3K   14  
This is the Second Part XML file as data source in Windows8
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Mvvm3_Basic.Model;
using System.Xml;
using System.Xml.Linq;
using Windows.ApplicationModel;
using Windows.Data.Xml.Dom;
using Windows.Storage;
using System.IO;

namespace Mvvm3_Basic.ViewModel
{
    class DB
    {
        public ObservableCollection<Contact> contactlist { get; set; }

        public async void GetContactList()
        {
            var sf = await Package.Current.InstalledLocation.GetFileAsync(@"ContactXML.xml");
            var file = await sf.OpenAsync(FileAccessMode.Read);
            Stream inStream = file.AsStreamForRead();

            XDocument xdoc = XDocument.Load(inStream);
            var contacts = (from contact in xdoc.Descendants("Contact")
                            select new
                            {
                                CGuid = contact.Element("CGuid").Value,
                                Name = contact.Element("Name").Value,
                                Email = contact.Element("Email").Value
                            });

            if (contactlist == null) contactlist = new ObservableCollection<Contact>();
            contactlist.Clear();

            foreach (var c in contacts)
            {
                contactlist.Add(new Contact { CGuid = c.CGuid, Name = c.Name, Email = c.Email });
            }
        }

        public async void Save(Contact c)
        {
            var sf = await Package.Current.InstalledLocation.GetFileAsync(@"ContactXML.xml");
            XmlDocument xmlDoc;
            using (var stream = await sf.OpenAsync(FileAccessMode.ReadWrite))
            {
                xmlDoc = await XmlDocument.LoadFromFileAsync(sf);
                XmlElement root = xmlDoc.DocumentElement;
                XmlElement xe = xmlDoc.CreateElement("Contact");
                XmlElement cguid = xmlDoc.CreateElement("CGuid");
                cguid.InnerText = Guid.NewGuid().ToString();
                XmlElement name = xmlDoc.CreateElement("Name");
                name.InnerText = c.Name;
                XmlElement email = xmlDoc.CreateElement("Email");
                email.InnerText = c.Email;
                xe.AppendChild(cguid);
                xe.AppendChild(name);
                xe.AppendChild(email);
                root.AppendChild(xe);
            }
            if (xmlDoc != null)
                await xmlDoc.SaveToFileAsync(sf);
        }

        public async void Update(Contact c)
        {
            var sf = await Package.Current.InstalledLocation.GetFileAsync(@"ContactXML.xml");
            XmlDocument xmlDoc;
            using (var stream = await sf.OpenAsync(FileAccessMode.ReadWrite))
            {
                xmlDoc = await XmlDocument.LoadFromFileAsync(sf);
                XmlElement root = xmlDoc.DocumentElement;
                IXmlNode xee = root.SelectSingleNode("//Contact/CGuid[.='" + c.CGuid + "']");
                xee.NextSibling.NextSibling.InnerText = c.Email;
                xee.NextSibling.InnerText = c.Name;
            }
            if (xmlDoc != null)
                await xmlDoc.SaveToFileAsync(sf);
        }

        public async void Delete(Contact c)
        {
            var sf = await Package.Current.InstalledLocation.GetFileAsync(@"ContactXML.xml");
            XmlDocument xmlDoc;
            using (var stream = await sf.OpenAsync(FileAccessMode.ReadWrite))
            {
                xmlDoc = await XmlDocument.LoadFromFileAsync(sf);
                XmlElement root = xmlDoc.DocumentElement;
                //IXmlNode xee = root.SelectSingleNode("//Contact/Name[.='" + c.Name + "'] | //Contact/Email[.='" + c.Email + "']");
                IXmlNode xee = root.SelectSingleNode("//Contact/CGuid[.='" + c.CGuid + "']");
                xee.ParentNode.RemoveChild(xee.NextSibling.NextSibling);
                xee.ParentNode.RemoveChild(xee.NextSibling);
                xee.ParentNode.RemoveChild(xee);

                IXmlNode clup = root.SelectSingleNode("//*[not(node())]");
                clup.ParentNode.RemoveChild(clup);
            }
            if (xmlDoc != null)
                await xmlDoc.SaveToFileAsync(sf);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO Addya Technologies
India India
"I am the CEO"
An Entrepreneur, Driving an Business Transformation Unit.

Comments and Discussions