XML settings won't save?

0 Gregory Bockus · August 25, 2015
Hello everyone, I am pretty new to Visual C. I could say that C# is my first language to start learning.
So anyways, I've been trying to make an Address Book program to enhance my C# skills.
I am now trying to save settings using an XML file.
Anyone know of a way to fix this? Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace AddressBook
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        List<Person> people = new List<Person>();
        private void button4_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void toolsToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            if (!Directory.Exists(path + "\\ Address Book - Greg"))
                Directory.CreateDirectory(path + "\\ Address Book - Greg");
            if (!File.Exists(path + "\\ Address Book - Greg\\settings.xml"))
            {
                //File.Create((path + "\\ Address Book - Greg\\settings.xml"));
                XmlTextWriter xW = new XmlTextWriter(path + "\\ Address Book - Greg\\settings.xml", Encoding.UTF8);
                xW.WriteStartElement("People");
                xW.WriteEndElement();
                xW.Close();
            }
        }

        private void addContactToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Person p = new Person();
            p.Name = textBox1.Text;
            p.Street = textBox3.Text;
            p.Email = textBox2.Text;
            p.Birthday = dateTimePicker1.Value;
            p.AdditionalNotes = textBox4.Text;
            people.Add(p);
            listView1.Items.Add(p.Name);
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            dateTimePicker1.Value = DateTime.Now;

        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                textBox1.Text = people[listView1.SelectedItems[0].Index].Name;
                textBox2.Text = people[listView1.SelectedItems[0].Index].Email;
                textBox3.Text = people[listView1.SelectedItems[0].Index].Street;
                textBox4.Text = people[listView1.SelectedItems[0].Index].AdditionalNotes;
                dateTimePicker1.Value = people[listView1.SelectedItems[0].Index].Birthday;
            }
            catch { }
        }

        private void removeContactToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Remove();
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            dateTimePicker1.Value = DateTime.Now;
        }
        void Remove()
        {
            try
            {
                listView1.Items.Remove(listView1.SelectedItems[0]);
                people.RemoveAt(listView1.SelectedItems[0].Index);
            }
            catch { }
        }

        private void removeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Remove();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //people[listView1.SelectedItems[0].Index].Name = textBox1.Text;
            //people[listView1.SelectedItems[0].Index].Email = textBox2.Text;
            //people[listView1.SelectedItems[0].Index].Street = textBox3.Text;
            //people[listView1.SelectedItems[0].Index].AdditionalNotes = textBox4.Text;
            //people[listView1.SelectedItems[0].Index].Birthday = dateTimePicker1.Value;
            //listView1.SelectedItems[0].Text = textBox1.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                people[listView1.SelectedItems[0].Index].Name = textBox1.Text;
                people[listView1.SelectedItems[0].Index].Email = textBox2.Text;
                people[listView1.SelectedItems[0].Index].Street = textBox3.Text;
                people[listView1.SelectedItems[0].Index].AdditionalNotes = textBox4.Text;
                people[listView1.SelectedItems[0].Index].Birthday = dateTimePicker1.Value;
                listView1.SelectedItems[0].Text = textBox1.Text;
            }
            catch
            {
                MessageBox.Show("Use the Add Contact feature to add a contact!");
            }
        }

        //private void button4_Click(object sender, EventArgs e)
        //{
        //    XmlDocument xDoc = new XmlDocument();
        //    string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        //    xDoc.Load(path + "\\ Address Book - Greg\\settings.xml");
        //}

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //XmlDocument xDoc = new XmlDocument();
            string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(path + "\\ Address Book - Greg\\settings.xml");
            XmlNode xNode = xDoc.SelectSingleNode("People");
            xNode.RemoveAll();
            foreach (Person p in people)
            {
                XmlNode xTop = xDoc.CreateElement("Person");
                XmlNode xName = xDoc.CreateElement("Name");
                XmlNode xEmail = xDoc.CreateElement("Email");
                XmlNode xStreet = xDoc.CreateElement("Street");
                XmlNode xNotes = xDoc.CreateElement("Notes");
                XmlNode xBirthday = xDoc.CreateElement("Birthday");
                xName.InnerText = p.Name; // xName is taken from above: "Name". Just with an "x" added infront for XML
                xEmail.InnerText = p.Email;
                xStreet.InnerText = p.Street;
                xNotes.InnerText = p.AdditionalNotes;
                xBirthday.InnerText = p.Birthday.ToFileTime().ToString();
                xTop.AppendChild(xName);
                xTop.AppendChild(xEmail);
                xTop.AppendChild(xStreet);
                xTop.AppendChild(xNotes);
                xTop.AppendChild(xBirthday);
                xDoc.DocumentElement.AppendChild(xTop);
            }
            xDoc.Save(path + "\\ Address Book - Greg\\settings.xml");
        }

        //private void button2_Click(object sender, EventArgs e)
        //{
        //    //XmlDocument xDoc = new XmlDocument();
        //    string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        //    XmlDocument xDoc = new XmlDocument();
        //    xDoc.Load(path + "\\ Address Book - Greg\\settings.xml");
        //    XmlNode xNode = xDoc.SelectSingleNode("People");
        //    xNode.RemoveAll();
        //    foreach (Person p in people)
        //    {
        //        XmlNode xTop = xDoc.CreateElement("Person");
        //        XmlNode xName = xDoc.CreateElement("Name");
        //        XmlNode xEmail = xDoc.CreateElement("Email");
        //        XmlNode xStreet = xDoc.CreateElement("Street");
        //        XmlNode xNotes = xDoc.CreateElement("Notes");
        //        XmlNode xBirthday = xDoc.CreateElement("Birthday");
        //        xName.InnerText = p.Name; // xName is taken from above: "Name". Just with an "x" added infront for XML
        //        xEmail.InnerText = p.Email;
        //        xStreet.InnerText = p.Street;
        //        xNotes.InnerText = p.AdditionalNotes;
        //        xBirthday.InnerText = p.Birthday.ToFileTime().ToString();
        //        xTop.AppendChild(xName);
        //        xTop.AppendChild(xEmail);
        //        xTop.AppendChild(xStreet);
        //        xTop.AppendChild(xNotes);
        //        xTop.AppendChild(xBirthday);
        //        xDoc.DocumentElement.AppendChild(xTop);
        //    }
        //    xDoc.Save(path + "\\ Address Book - Greg\\settings.xml");
        //}
    }

    class Person
    {
        public string Name
        {
            get;
            set;
        }
        public string Email
        {
            get;
            set;
        }
        public string Street
        {
            get;
            set;
        }
        public string AdditionalNotes
        {
            get;
            set;
        }
        public DateTime Birthday
        {
            get;
            set;
        }
    }
}
It will not save the settings in the settings.xml
Thanks everyone! :)

Post a Reply

Replies

Oldest  Newest  Rating
0 jethro daniel · August 25, 2015
First of all, when using system resources, you must learn to close/dispose it after use. Also after all editing, call the save method. As I can see from your code, there are multiple instances of the xmldocument class. You can use one, and dispose of it after use
  • 1

C#

107,202 followers
About

C# is an object-oriented language designed for improving the development of web applications.

Links
Moderators