Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am developing an application which list the data from an xml file, and user can add more entries into this xml file. Later, when this list is again populated, newer entries will be displayed back along with older ones.
My xml file is in my current project folder, & its "BUILD ACTION" is set as "CONTENT"
& "COPY TO OUTPUT DIRECTORY" is "COPY ALWAYS".
Now, when i run the project, this xml file gets copied into "Debug" folder. And then this file in Output directory gets modified and updated and all values are displayed fine.
Now the problem s that, when i restart the project, again this file is copied to Debug folder & all updates are lost....
Kindly suggest ways in which i can use the same file and modify it, so that no change made is lost...
Also, if i specify the name of file as (@"C:\file.xml"), the it show "Object reference not set to an instance of an object" error.
Thanks in advance....
Posted

1 solution

Don't include the XML file in your project, or change it so that's it's not automatically copied to the output directory. Then you'll need to modify your app so that it can handle the case where the file doesn't exist yet and create it with some default values in it. If you want to include the config file with your app when you deploy it to another PC then you can include that file when you build the setup and deployment project.

If you specify the full path to a file and then that file doesn't exist, an exception may thrown when you try to load it. I say may because it depends on the exact way you're doing the loading - some ways throw and others don't. A null ref exceptions sounds like the load operation is returning null and then you aren't checking the document variable for null before using it.

Also this sounds like a file that should be stored in the App Data or Local App Data folder, not alongside the executable. If your app is deployed to the Program Files directory then you'll have problems reading and writing it without granting unnecessary privileges to your app.
 
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