0

I am working on a winforms app using C# and wanted to add a save feature to a few parts where the user would be bale to enter text into a textbox and have it saved for next time.

how is this implemented in winforms? I am trying a local xml file and failing to persists the text while being able to read it. data.xml is the local file in my project root folder.

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"(full path)\visual studio 2015\Projects\My_helper\data.xml");

string subject = xmlDoc.DocumentElement.SelectSingleNode(@"./content/reminder_email/subject").InnerText.ToString();

string body = xmlDoc.DocumentElement.SelectSingleNode(@"./content/reminder_email/body").InnerText.ToString();
kacalapy
  • 9,806
  • 20
  • 74
  • 119
  • 1
    If there are only few things to be saved I would suggest you to use [Property Settings](https://stackoverflow.com/questions/650555/how-to-use-settings-in-visual-c-sharp) – boop_the_snoot Aug 15 '17 at 16:50
  • What should be structure of your xml and under which node you want to save data? Did take a look at [this SO](https://stackoverflow.com/questions/9616163/c-sharp-modify-a-xml-node) or [this SO](https://stackoverflow.com/questions/16806838/how-to-update-xml-nodes-with-new-values) ? – Siva Gopal Aug 15 '17 at 16:54
  • Possible duplicate of [C# : Modify a xml node](https://stackoverflow.com/questions/9616163/c-sharp-modify-a-xml-node) – Siva Gopal Aug 15 '17 at 16:55

2 Answers2

1

Using settings is probably the easiest approach.

Read: https://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx

  • Create a setting in the solution explorer as a string type
  • Assign using Properties.Settings.Default.Your Setting Name = TextBox1.Text
  • Read using TextBox1.Text = Properties.Settings.Default.Your Setting Name

Don't forget to call Properties.Settings.Default.Save(); after changing a setting.

0liveradam8
  • 752
  • 4
  • 18
0

The XML approach is a good one, I've used it a lot. But beware where you choose to store it. For instance, if your application is installed to ProgramFiles, you won't be able to write there. You'd have to store it in ProgramData, or in the user's space.

As for the application settings, it's not really a bad way to go, but it's also not meant to be data storage. So if it's really data you're storing, use something meant for that purpose: a database or at least XML.

DonBoitnott
  • 10,787
  • 6
  • 49
  • 68