Click here to Skip to main content
12,950,084 members (54,469 online)
Click here to Skip to main content


30 bookmarked
Posted 28 Aug 2009

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

, 28 Aug 2009 CPOL
Outlook type Address Book in C#, LINQ, XML with Menu and ToolBar
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
                if (_contactList == null)
                    _contactList = new List<Contacts>();

                return _contactList;


        public static string XMLFilePath
                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,


            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;


            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();


           //changes done are only in memory so go ahead and save it.

            foreach (Contacts cts in ContactList)
                if (cts.Email == emailAddress)


       /// <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.

               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.

       /// <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);


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.


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


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.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 28 Aug 2009
Article Copyright 2009 by Nadeem Alvi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid