12

I'm learning C# by writing a home library manager.

I have a BookController that will store the books in a data structure and perform operations on them.

Does C# have a way of saving the data in the dictionary to a local file perhaps in an XML fashion to load later, or am I going to have to write it myself?

What is the best method of saving and loading this data available to C#? Just need pointed in a good direction.

gius
  • 9,289
  • 3
  • 33
  • 62
Präriewolf
  • 829
  • 2
  • 9
  • 28

5 Answers5

24

Actually, C# (the language) doesn't know anything about serialization, but .NET (the framework) provides lots of ways... XmlSerializer, BinaryFormatter, DataContractSerializer (.NET 3.0) - or there are a few bespoke serialization frameworks too.

Which to use depends on your requirements; BinaryFormatter is simple to use, but burns assembly metadata information into the file - making it non-portable (you couldn't open it in Java, for example). XmlSerializer and DataContractSerializer are primarily xml-based, making it quite portable, but fairly large unless you compress it.

Some proprietary serializers are half-way between the two; protobuf-net is a binary formatter (so very dense data), but which follows a portable standard for the data format (Google's protocol buffers spec). Whether this is useful depends on your scenario.

Typical code (here using XmlSerializer):

        XmlSerializer ser = new XmlSerializer(typeof(Foo));
        // write
        using (var stream = File.Create("foo.xml"))
        {
            ser.Serialize(stream, foo); // your instance
        }
        // read
        using (var stream = File.OpenRead("foo.xml"))
        {
            Foo newFoo = (Foo)ser.Deserialize(stream);
        }
dbc
  • 104,963
  • 20
  • 228
  • 340
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
6

Look at Serialization and the ISerializable interface

It allows you to save an object's state in a portable form.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
1

If you want your data to be in XML format there's XmlSerializer.

EDIT: For some reason I was downvoted for suggesting this because XmlSerializer cannot serialize a Dictionary.

To get around this problem you can always customize the serialization process like this

Cameron MacFarland
  • 70,676
  • 20
  • 104
  • 133
  • XmlSerializer is not able to serialize Dictionary. – Dario Solera Dec 16 '08 at 07:46
  • Who said this is what @Coyote is using? No need to down-vote, the answer is not *wrong*. Incomplete maybe, but then a hint alone suffices. There are ways to XML serialize a dictionary, if you hit that wall. – Tomalak Dec 16 '08 at 07:53
  • I see a dictionary as an internal look up optimization that doesn't need to be exposed in the data. i.e. if you dictionary key is a property in your object, then you are just bloating the payload by having it there twice and having the extra wrapper code. IMO it is lazy drag-drop coder practice to serialize dictionaries. – Lee Campbell Apr 12 '13 at 14:07
1

You should also consider looking at Linq to SQL (or Entity Framework) for O/R mapping and storing your data into a database (SQL CE, SQL Express).

gius
  • 9,289
  • 3
  • 33
  • 62
1

I've created an extension method which takes an object and creates an xml string for you, all you need to do is save/load from file. XmlSerialisation

Community
  • 1
  • 1
TWith2Sugars
  • 3,384
  • 2
  • 26
  • 43