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?
-
3How come you want to optimize storage efficiency specifically? Is it for the learning experience, or actually for the coobook application? To me that doesn't sound like an application that needs the most optimized storage. Besides.. in what way do you want it to be optimized? Low storage space, ease of use/readability, portability, performance? – Gaute Løken Oct 09 '13 at 19:12
-
Why not go ahead and make multiple implementations using SQL and XML separately? Sounds like this is strictly for learning, not school/work, so no time constraint, right? – sab669 Oct 09 '13 at 19:14
-
I am sorry if I wrote something I shouldn't, I see people gave me minus. No, I don't want to make any application for now, I just want to practice. Nobody needs cookbook app for that matter. I am interested just how you can keep data after the application is closed. – Biljana M. Oct 09 '13 at 20:17
-
Make your models agnostic to begin with. – Gayot Fow Oct 09 '13 at 23:27
-
Why is this such problem question? I have to learn somehow, reading people's opinions seems like a good way and I appreciate all the answers. – Biljana M. Oct 23 '13 at 07:11
6 Answers
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:
string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
So if you want to store your data in a folder called "Cookbook" and a file called "recipes.xml" you could do the following:
string dataPath = Path.Combine(appDataPath, "Cookbook");
string recipesFileFullPath = Path.Combine(dataPath, "recipes.xml");
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 like File
and FileStream
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 older XmlSerializer
.
Edit:
Here's some sample code to create an object and save it to an XML file:
public class RecipeBook
{
public List<Recipe> Recipes { get; set; }
public RecipeBook()
{
Recipes = new List<Recipe>();
}
}
public class Recipe
{
public DateTime LastModified { get; set; }
public DateTime Created { get; set; }
public string Instructions { get; set; }
}
public void SomeFunction()
{
RecipeBook recipeBook = new RecipeBook();
var myRecipe = new Recipe()
{
Created = DateTime.Now,
LastModified = DateTime.Now,
Instructions = "This is how you make a cake."
};
recipeBook.Recipes.Add(myRecipe);
var doc = new XDocument();
using (var writer = doc.CreateWriter())
{
var serializer = new XmlSerializer(typeof(RecipeBook));
serializer.Serialize(writer, recipeBook);
}
doc.Save(recipesFileFullPath);
}
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 the recipesFileFullPath
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 new RecipeBook
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:
var serializer = new XmlSerializer(typeof(RecipeBook));
using (var fs = new FileStream(recipesFileFullPath))
{
RecipeBook book = (RecipeBook)serializer.Deserialize(fs);
}

- 11,292
- 11
- 63
- 102
-
I would +1 your answer but unfortunately I can't I am too new here. – Biljana M. Oct 10 '13 at 06:04
-
Trevor, how would you read data from file into recepieBook using this XDocument and XmlSerializer? I am very interested in this solution. thanks! – Biljana M. Oct 22 '13 at 19:31
-
1Edited my answer. You should mark the answer as accepted if it answers your question. – Trevor Elliott Oct 22 '13 at 23:31
-
Thank you, Trevor, this works fantastic and I have learnt how to put and get data from xml file which is great! – Biljana M. Oct 27 '13 at 17:08
-
-
Good answer. For deserializing I had to pass a `FileMode` enum to construct the `FileStream` :) – A1rPun Nov 04 '16 at 19:35
I would suggest using a localDB in SqlExpress.
Microsoft® SQL Server® 2012 Express is a powerful and reliable free data management system that delivers a rich and reliable data store for lightweight Web Sites and desktop applications.
http://blogs.msdn.com/b/sqlexpress/archive/2011/07/12/introducing-localdb-a-better-sql-express.aspx
http://technet.microsoft.com/en-us/library/hh510202.aspx
You could also look at this similar question.
Here as an article on XML vs. DB which shows XML is losing ground.
-
I would love to give +1 but I don't have enough of "reputation" to + anyone. – Biljana M. Oct 10 '13 at 06:02
try to use SQLite it's lightweight portable database http://www.sqlite.org/

- 13,492
- 1
- 36
- 47
Your best bet would be to use Sql Server Compact (http://www.microsoft.com/en-us/sqlserver/editions/2012-editions/compact.aspx1).
That is because it comes embedded with your application and you don't need to install extra libraries on deployments etc. Also it is a good storage point for simple tabular data.

- 6,829
- 2
- 35
- 42
It depends a lot of how much data the application will hold and how you want to search for that data.
For smaller amounts of data a plain text file containing XML och JSON will do. For larger amounts of data i recommend SQL server or a NOSQL-database (MongoDB or Raven).

- 69
- 3
This depends on a lot of things, mostly how many recipes you plan on having and if you want to be able to search, sort, or style the recipes.
Text files are the simplest way. You could add some simple search and ordering functionality.
If it's in this hundreds, you could get by with using xml and a serializer. It would allow you to later style the recipes and it gives the data some structure. If your recipes enter the thousands, this way may become a nightmare.
The best way would be a database. Sql Server Compact is a free option that links up nicely with c# through something like Entity Framework. This option will take the longest by far if you're not already familiar with databases or ado.net or entity framework.

- 3,499
- 2
- 25
- 39
-
THat would be for learning purposes only, and i am just interested in how I could keep data outside application to stay, like in database. As I could see, interaction between c# and database is very complex, so I thought if there's a way to do it in easier way, that would probably be text file. I am still kinda confused by XML and C# interaction. – Biljana M. Oct 09 '13 at 20:22