0

I'm writing a project most for fun, just to play around with .NET. I'm building an xaml project (or what you call the windows 8 apps) in C#.

In this case I will have a bigger object with some lists of other abojects and stuff. Is there any smart way of saving these object to disk for loading them later? That is something like GetMyOldSavesObectWithName("MyObject");

What i've read that the local storage is primary used to save smaller thing, such as settings. How much data is acceptale to save? Can it handle a bigger object, and what are the pros/cons? Should i save the objects to files, and how do i in that case do that? Is there any smart way of telling .net to "Save this object to MyObjectName.xml" or something?

gubbfett
  • 2,157
  • 6
  • 32
  • 54
  • 1
    You can serialize information or write data manually, size limits are not an issue with modern hardware. – Cyral Jan 14 '15 at 20:53
  • What do you consider "large" and why are you storing one in a windows 8 app? – BradleyDotNET Jan 14 '15 at 20:54
  • Why so opposed to using a database? Windows phone is setup to use SQL and LINQ out of the box, so why fight it? http://msdn.microsoft.com/en-us/library/windows/apps/hh202860(v=vs.105).aspx – Lee Harrison Jan 14 '15 at 21:05
  • @BradleyDotNET "large" are something like an object (lets say Party) with a (string) name, a list<> of the object People (maybe 3-4 string values in that object maybe), a list<> of the object Drinks (etc etc). hard to say in kb/mb, but you get the figure? – gubbfett Jan 14 '15 at 21:06
  • @Cyral: any more specific how to achieve that? that is, how to save the party object, and be able to load it later. i guess you mean something like serializing the object to a string and save it to a file, and then load it? like Party MyParty = loadThisBabyFromFile("Myparty.xml")? – gubbfett Jan 14 '15 at 21:06
  • Thats not very large at all. Serializing it to XML wouldn't be a problem. – BradleyDotNET Jan 14 '15 at 21:07
  • @LeeHarrison because a database feels kind of overkill for this project. – gubbfett Jan 14 '15 at 21:08
  • @LeeHarrison Standard windows 8 actually doesn't have much database support (about the best you can get is an SQLLite extension) – BradleyDotNET Jan 14 '15 at 21:08
  • @BradleyDotNET well nah, the amount af data is'nt that much, but it feels a little too much for saving it like a localsetting or something. :) the question is more what options i have, saving a file for each "party" is one way, and from the aswers i've got here it feels like thats a pretty good way of doing it? – gubbfett Jan 14 '15 at 21:11
  • Thats what I would do :) – BradleyDotNET Jan 14 '15 at 21:16

1 Answers1

2

In C#, you can use serialization to save your objects as files, commonly in XML format. Other libraries, such as JSON.net, can serialize into JSON.

You could also roll out your own saving/loading format, which will probably run faster and store data in a more compact way, but will take much more time on your part. This can be done with BinaryReaders and Writers.

Take a look at this StackOverflow answer if you wish to go the serialization route.

In most cases data will be so compact it will not use much space at all. Based on your comment, that "large" amount of data would really only take a few KBs.

Community
  • 1
  • 1
Cyral
  • 13,999
  • 6
  • 50
  • 90
  • That's a good thread, thanks. A smaller object, in my mind, could be stored in the ApplicationData.Current.LocalSettig imo, that would keep me from saving a bunch of files, but these KBs of data feels kind of big to save like this. For me this is something like the windows registry, right? therefore, is there anything else built in to use for saving larger stuff without saving them as files? – gubbfett Jan 14 '15 at 21:17
  • These are not _settings_, and they contain too complex data for the registry or LocalSettings. Most applications save data to the disk, and the amount you want to save is actually very little, so you should not worry about doing so. The kind of data you want to save is not suited for the registry however. I wouldn't save that many separate files, but pack most of it into one larger file. – Cyral Jan 14 '15 at 21:19
  • Well yea, that was my point. That the registry is not used/suited for that. Therefore i was wondering if there was some kind of other suitable built in "local storage" stuff for saving these kind of stuff. But i guess there's not then. Files will do just fine. Thanks. – gubbfett Jan 14 '15 at 21:23