0

I have been working with xml for the first time and have gotten stuck with the saving/ loading aspect of it. I want the information from the text boxes to be saved to the xml file when the app closes, however when the app runs I can not close the app with the "X' in the top right corner. I have tried deleting the xml file and recreating it, but that still doesnt help.

this is the save function...........

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            XmlDocument xDoc = new XmlDocument();
            string place = "\\ClientMeasurementLog\\settings.xml";
            string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            xDoc.Load(path + place);

            XmlNode xNode = xDoc.SelectSingleNode("ClientMeasurements");
            xNode.RemoveAll();

            foreach (Client c in client)
            {
                XmlNode xTop = xDoc.CreateElement("Client");
                XmlNode xName = xDoc.CreateElement("Name");
                XmlNode xInitialForearm = xDoc.CreateElement("InitialForearm");
                XmlNode xInitialUpperArmR = xDoc.CreateElement("InitialUpperArmRight");
                XmlNode xInitialUpperArmL = xDoc.CreateElement("InitialUpperArmLeft");
                XmlNode xInitialChest = xDoc.CreateElement("InitialChest");
                XmlNode xInitialWaist = xDoc.CreateElement("InitialWaist");
                XmlNode xInitialHips = xDoc.CreateElement("InitialHips");
                XmlNode xInitialThighR = xDoc.CreateElement("InitialThighRight");
                XmlNode xInitialThighL = xDoc.CreateElement("InitialThighLeft");
                XmlNode xInitialCalfR = xDoc.CreateElement("InitialCalfRight");
                XmlNode xInitialCalfL = xDoc.CreateElement("InitialCalfLeft");
                XmlNode xMostRecentForearm = xDoc.CreateElement("MostRecentForearm");
                XmlNode xMostRecentUpperArmR = xDoc.CreateElement("MostRecentUpperArmRight");
                XmlNode xMostRecentUpperArmL = xDoc.CreateElement("MostRecentUpperArmLeft");
                XmlNode xMostRecentChest = xDoc.CreateElement("MostRecentChest");
                XmlNode xMostRecentWaist = xDoc.CreateElement("MostRecentWaist");
                XmlNode xMostRecentHips = xDoc.CreateElement("MostRecentHips");
                XmlNode xMostRecentThighR = xDoc.CreateElement("MostRecentThighRight");
                XmlNode xMostRecentThighL = xDoc.CreateElement("MostRecentThighLeft");
                XmlNode xMostRecentCalfR = xDoc.CreateElement("MostRecentCalfRight");
                XmlNode xMostRecentCalfL = xDoc.CreateElement("MostRecentLeft");

                xName.InnerText = c.Name;
                xInitialForearm.InnerText = c.InitialForearm;
                xInitialUpperArmR.InnerText = c.InitialUpperArmR;
                xInitialUpperArmL.InnerText = c.InitialUpperArmL;
                xInitialChest.InnerText = c.InitialChest;
                xInitialWaist.InnerText = c.InitialWaist;
                xInitialHips.InnerText = c.InitialHips;
                xInitialThighR.InnerText = c.InitialThighR;
                xInitialThighL.InnerText = c.InitialThighL;
                xInitialCalfR.InnerText = c.InitialCalfR;
                xInitialCalfL.InnerText = c.InitialCalfL;
                xMostRecentForearm.InnerText = c.MostRecentForearm;
                xMostRecentUpperArmR.InnerText = c.MostRecentUpperArmR;
                xMostRecentUpperArmL.InnerText = c.MostRecentUpperArmL;
                xMostRecentChest.InnerText = c.MostRecentChest;
                xMostRecentWaist.InnerText = c.MostRecentWaist;
                xMostRecentHips.InnerText = c.MostRecentHips;
                xMostRecentThighR.InnerText = c.MostRecentThighR;
                xMostRecentThighL.InnerText = c.MostRecentThighL;
                xMostRecentCalfR.InnerText = c.MostRecentCalfR;
                xMostRecentCalfL.InnerText = c.MostRecentCalfL;

                xTop.AppendChild(xName);
                xTop.AppendChild(xInitialForearm);
                xTop.AppendChild(xInitialUpperArmR);
                xTop.AppendChild(xInitialUpperArmL);
                xTop.AppendChild(xInitialChest);
                xTop.AppendChild(xInitialWaist);
                xTop.AppendChild(xInitialHips);
                xTop.AppendChild(xInitialThighR);
                xTop.AppendChild(xInitialThighL);
                xTop.AppendChild(xInitialCalfR);
                xTop.AppendChild(xInitialCalfL);
                xTop.AppendChild(xMostRecentForearm);
                xTop.AppendChild(xMostRecentUpperArmR);
                xTop.AppendChild(xMostRecentUpperArmL);
                xTop.AppendChild(xMostRecentChest);
                xTop.AppendChild(xMostRecentWaist);
                xTop.AppendChild(xMostRecentHips);
                xTop.AppendChild(xMostRecentThighR);
                xTop.AppendChild(xMostRecentThighL);
                xTop.AppendChild(xMostRecentCalfR);
                xTop.AppendChild(xMostRecentCalfL);
                xDoc.DocumentElement.AppendChild(xTop);

            }

            xDoc.Save(path + place);

        }

After stepping through the program in debug mode I noticed it always stops here.........

 XmlDocument xDoc = new XmlDocument();
                string place = "\\ClientMeasurementLog\\settings.xml";
                string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
                xDoc.Load(path + place);

However, im not sure why it just stops there.

3
Contributors
2
Replies
14
Views
3 Years
Discussion Span
Last Post by Fenrir()
0

Hi,

Most likely, the form is not closing when you hit "X" button because you get an exception inside Form1_FormClosing.

I suggest you call Form1_FormClosing method explicitly while the app is still running to see what type of exception you get. You can just drop a new test button and call the method "onClick" of that button. You may use the following line to call the method:

Form1_FormClosing(this, new FormClosingEventArgs(CloseReason.None, true));

Hope this helps.

Edited by The Diamonds

0

Try implementing some exception handling in your code aswell. Also ensure that you have permission to access the appdata folder

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.