Click here to Skip to main content
15,922,696 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my project I have got a listview filled with contacts which are stored in a XML file and this is the connection between the two of these. Basically this is how the contacts are loaded:

Please note that first function is called on form loading:

C#
void LoadContacts()
    {
        string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        string phonebook_path = path + "\\Phonebook\\Contacts.xml";
        if (!File.Exists(phonebook_path))
        {
            XmlTextWriter xW = new XmlTextWriter(phonebook_path, Encoding.UTF8);
            xW.WriteStartElement("People");
            xW.WriteEndElement();
            xW.Close();
        }
        XmlDocument xDoc = new XmlDocument();
        xDoc.Load(phonebook_path);
        foreach (XmlNode xNode in xDoc.SelectNodes("People/Person"))
        {
            Person p = new Person();
            p.Name = xNode.SelectSingleNode("Name").InnerText;
            p.Hometown = xNode.SelectSingleNode("Hometown").InnerText;
            p.Address = xNode.SelectSingleNode("Address").InnerText;
            p.Birthday = DateTime.FromFileTime(Convert.ToInt64(xNode.SelectSingleNode("Birthday").InnerText));
            p.Phone = xNode.SelectSingleNode("Phone").InnerText;
            p.Email = xNode.SelectSingleNode("Email").InnerText;
            p.AdditionalInfo = xNode.SelectSingleNode("AdditionalInfo").InnerText;
            people.Add(p);
            listView1.Items.Add(p.Name);
            UserCount();
        }
    }

and then the other one on form closing:
C#
private void Main_FormClosing(object sender, FormClosingEventArgs e)
    {
        XmlDocument xDoc = new XmlDocument();
        string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        string phonebook_path = path + "\\Phonebook\\Contacts.xml";
        xDoc.Load(phonebook_path);
        XmlNode xNode = xDoc.SelectSingleNode("People");
        xNode.RemoveAll();
        foreach (Person p in people)
        {
            XmlNode xTop = xDoc.CreateElement("Person");
            XmlNode xName = xDoc.CreateElement("Name");
            XmlNode xHometown = xDoc.CreateElement("Hometown");
            XmlNode xAddress = xDoc.CreateElement("Address");
            XmlNode xBirthday = xDoc.CreateElement("Birthday");
            XmlNode xPhone = xDoc.CreateElement("Phone");
            XmlNode xEmail = xDoc.CreateElement("Email");
            XmlNode xAdditionalInfo = xDoc.CreateElement("AdditionalInfo");
            xName.InnerText = p.Name;
            xHometown.InnerText = p.Hometown;
            xAddress.InnerText = p.Address;
            xBirthday.InnerText = p.Birthday.ToFileTime().ToString();
            xPhone.InnerText = p.Phone;
            xEmail.InnerText = p.Email;
            xAdditionalInfo.InnerText = p.AdditionalInfo;
            xTop.AppendChild(xName);
            xTop.AppendChild(xHometown);
            xTop.AppendChild(xAddress);
            xTop.AppendChild(xBirthday);
            xTop.AppendChild(xPhone);
            xTop.AppendChild(xEmail);
            xTop.AppendChild(xAdditionalInfo);
            xDoc.DocumentElement.AppendChild(xTop);
        }

        xDoc.Save(phonebook_path);
        Sync();
        SetStartup();
    }

C#
I have got an option to import the XML file via method Import() which is called when a button is clicked.
void Import()
    {
        Main f1 = new Main();
        OpenFileDialog BrowseFile = new OpenFileDialog();
        if (BrowseFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            Properties.Settings.Default.ImportPath = BrowseFile.FileName;
        }
        string fileName = "Contacts.xml";
        string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        string sourcePath = Properties.Settings.Default.ImportPath;
        string targetPath = path + "\\Phonebook\\";
        string destFile = System.IO.Path.Combine(targetPath, fileName);
        System.IO.File.Copy(sourcePath, destFile, true); 
        MessageBox.Show("Contacts have successfully been imported. Please restart your application in order changes to take effect!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

...Importing is performed perfectly which means I can see the copied XML file in the right place, but when I close the application, all of a sudden XML file turns into previous state somehow which means changes didn't take effect - when I load the app again, there will be no imported contacts.

How can I solve this issue?
Posted
Comments
AnthonyMG 30-Nov-13 2:41am    
Copied file should be saved again to retain the changed state.

You forgot to save the new value of the Properties.Settings:
C#
Properties.Settings.Default.ImportPath = BrowseFile.FileName;
Properties.Settings.Default.Save();
If you don't do that the info isn't saved between application runs, so it picks up the old path info.
 
Share this answer
 
Comments
Exinta Ennea 30-Nov-13 8:19am    
There is no need to save that particular setting, since it copies the file perfectly without doing so. Loading that "new" XML is actually the problem.
For more visit here..

How to import XML data in c#[^]
 
Share this answer
 
This is what I came up with:

C#
void Import()
        {
            XmlDocument xDoc = new XmlDocument();
            OpenFileDialog BrowseFile = new OpenFileDialog();
            if (BrowseFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Properties.Settings.Default.ImportPath = BrowseFile.FileName;
                Properties.Settings.Default.Save();
            }
            string fileName = "Contacts.xml";
            string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            string sourcePath = Properties.Settings.Default.ImportPath;
            string targetPath = path + "\\Phonebook\\";
            string destFile = System.IO.Path.Combine(targetPath, fileName);
            System.IO.File.Copy(sourcePath, destFile, true);
            xDoc.Save(destFile);
            MessageBox.Show("Contacts have successfully been imported. Please restart your application in order changes to take effect!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }


It actually loads contacts after the app is restarted, but the problem is I am getting an error when I perform Import().

This is what the error is about:

IMAGE[^]

So, after that I close the application, run it again and the contacts are imported.

Any ideas?
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900