C# – the best way to store data in c# application

cdatasetdatasourceoopstorage

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?

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:

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);
}