I want to make Cookbook application for storing and reading (and updating) recipes, or anything else to practice OOP programming and thinking. But, I am not sure, what way of storing data, in this case, recipes, is the best in c# (Visual Studio Express).
I want to optimize saving and loading data in program, but I have no experience. What is the best way? Is it through XML, SQL, or just plain TXT? Or some other way?
C# – the best way to store data in c# application
cdatasetdatasourceoopstorage
Related Topic
- C# – What are the correct version numbers for C#
- C# – the difference between a field and a property
- The difference between an abstract method and a virtual method
- Python – What are the differences between type() and isinstance()
- The difference between an interface and abstract class
- Python – the purpose of the word ‘self’
- Scala: list.flatten: no implicit argument matching parameter type (Any) = > Iterable[Any] was found
Best Answer
If you haven't done it yet it would be best to start with XML file input/output before getting into anything too advanced.
Normally you would read and write to files by using the following method to get the path:
So if you want to store your data in a folder called "Cookbook" and a file called "recipes.xml" you could do the following:
This gives you a path like
C:\Users\John\AppData\Local\Cookbook\recipes.xml
or something similar which you can pass to file input and output functions.Then you can get started with the
System.IO
namespace classes likeFile
andFileStream
to learn how to properly open and read/write to files.Then the next higher level step is to pass these file streams to something used to read and write XML to objects, such as Linq to XML (the
XDocument
class) which is the preferred approach. Or the olderXmlSerializer
.Edit:
Here's some sample code to create an object and save it to an XML file:
You would just need to break this code out into a structure that works for you. For example, if you were making a Windows Forms application then the
RecipeBook
would be a private member variable of your main form. In the constructor you could construct therecipesFileFullPath
string and store it as a private member variable too. On the Form.Loaded event you could check if the XML file already exists and if so load it. If not you would create a newRecipeBook
class that's empty. You would also probably only serialize and save when the user clicks a save button or when the Form.Closing event is raised.EDIT:
To deserialize and read from a file you can do the following: