Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Outlook Type Address Book in C#, LINQ, XML with Menu and ToolBar

, 28 Aug 2009
Outlook type Address Book in C#, LINQ, XML with Menu and ToolBar
AddressBook.zip
AddressBook
AddressBook
bin
Debug
AddressBook.exe
AddressBook.vshost.exe
AddressBook.vshost.exe.manifest
Properties
Settings.settings
Simon.ico
AddressBook.suo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Windows.Forms;

namespace AddressBook
{
   public static class XMLParse
    {
        private static List<Contacts> _contactList;
        public static List<Contacts> ContactList
        {
            get
            {
                if (_contactList == null)
                    _contactList = new List<Contacts>();

                return _contactList;
            }

        }


       
        public static string XMLFilePath
        {
            get
            {
                return Application.StartupPath + "\\AddressData.xml";
            }
        }

        static XDocument xmlDoc;
        public static bool getAllAddress()
        {
            //here i have to read the address data from the xml file.
            xmlDoc = XDocument.Load(XMLFilePath);

              var addresses = from Address in xmlDoc.Descendants("Address")
                            select new
                            {
                                FirstName = Address.Element("FirstName").Value,
                                LastName = Address.Element("LastName").Value,
                                Email = Address.Element("Email").Value,
                                Address1 = Address.Element("Address1").Value,
                                Address2 = Address.Element("Address2").Value,
                                City = Address.Element("City").Value,
                                Country = Address.Element("Country").Value,
                                Zip = Address.Element("Zip").Value,
                                Phone1 = Address.Element("Phone1").Value,
                                Phone2 = Address.Element("Phone2").Value,
                                State = Address.Element("State").Value,

                            };

          ContactList.Clear();
            foreach (var address in addresses)
            {
                Contacts contact = new Contacts();
                contact.FName = address.FirstName;
                contact.LName = address.LastName;
                contact.Email = address.Email;
                contact.Address1 = address.Address1;
                contact.Address2 = address.Address2;
                contact.City = address.City;
                contact.State = address.State;
                contact.Country = address.Country;
                contact.Phone1 = address.Phone1;
                contact.Phone2 = address.Phone2;

                ContactList.Add(contact);
            }

            return true;
        }

       /// <summary>
       /// Delete the existing contact from the list and from the xml file.
       /// </summary>
       /// <param name="emailAddress"></param>
       public static void deleteContact(string emailAddress)
       {
          
           XElement element = xmlDoc.Root.Elements("Address").Where(r => (string)r.Element("Email") == emailAddress).FirstOrDefault();

           if(element!=null)
           element.Remove();

           //changes done are only in memory so go ahead and save it.
            xmlDoc.Save(XMLFilePath);

            foreach (Contacts cts in ContactList)
            {
                if (cts.Email == emailAddress)
                {
                    _contactList.Remove(cts);
                    break;
                }
            }

       }


       /// <summary>
       /// Add new contact to the xml file and to the List.
       /// </summary>
       /// <param name="contact"></param>
       public static void AddContact(Contacts contact)
       {
           //Here we are going to add the new contact to the xml file.

           xmlDoc.Root.Add(
               new XElement("Address",                   
                     new XElement("FirstName", contact.FName),
                     new XElement("LastName", contact.LName),
                      new XElement("Email", contact.Email),
                       new XElement("Address1", contact.Address1),
                        new XElement("Address2", contact.Address2),
                         new XElement("City", contact.City),
                          new XElement("Country", contact.Country),
                           new XElement("Zip", contact.Zip),
                            new XElement("Phone1", contact.Phone1),
                            new XElement("Phone2", contact.Phone2),
                            new XElement("State", contact.State)                                  
                            ));
           //The changes are in memory so go ahead ab=nd save it to the xml file.
           xmlDoc.Save(XMLFilePath);
       
       
          
       }

       /// <summary>
       /// Update the xml file with new values. Email address cannot be updated as that is a primary key.
       /// </summary>
       /// <param name="contact"></param>
       public static void UpdateContact(Contacts contact)
       {
           XElement element = xmlDoc.Root.Elements("Address").Where(r => (string)r.Element("Email") == contact.Email).FirstOrDefault();

           if (element != null)
           {               
                    element.SetElementValue("FirstName", contact.FName);
                    element.SetElementValue("LastName", contact.LName);
                    element.SetElementValue("Email", contact.Email);
                    element.SetElementValue("Address1", contact.Address1);
                    element.SetElementValue("Address2", contact.Address2);
                    element.SetElementValue("City", contact.City);
                    element.SetElementValue("Country", contact.Country);
                    element.SetElementValue("Zip", contact.Zip);
                    element.SetElementValue("Phone1", contact.Phone1);
                    element.SetElementValue("Phone2", contact.Phone2);
                    element.SetElementValue("State", contact.State);

                    xmlDoc.Save(XMLFilePath);
           }
       }
    }
}

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)

Share

About the Author

Nadeem Alvi
Software Developer Shell Oil
United States United States
I am working as a Senior Software Developer with Shell Oil,Houston Texas USA.

| Advertise | Privacy | Mobile
Web04 | 2.8.140905.1 | Last Updated 28 Aug 2009
Article Copyright 2009 by Nadeem Alvi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid