Batteredburrito
Batteredburrito

Reputation: 589

UWP/C# Advice with storing application user data?

I need a bit of advice/opinions on storing data.

Im looking at essentially storing user input data and then reference it on a frequent basis. I would like to avoid data base storage as this is all going to be referenced locally. Ive seen that you can do XML serialised objects etc and wanted to know what you think about data storage.

If i give you an idea of what im looking at doing in a step by step it may give a better idea of what im trying to achieve:

Its a bit of a big task and i was looking at doing it all using XML. Im not sure if this is the best way to do it and wanted your opinions. Mainly due to the way the data can dynamically adjust all the time through user input.

Any input on this would be appreciated. I can provide images of what im trying to achieve if necessary.

Upvotes: 2

Views: 511

Answers (1)

TheBeardedQuack
TheBeardedQuack

Reputation: 449

I personally use Newtonsoft JSON for saving my user data. You can build all your classes as normal and then fairly easily integrate the JSON serialiser/deserialiser without too much hassle. There will be some exceptions that may require a custom serialiser, but it's not too difficult to make those, especially if you only need to interpret one small part and then can just pass the rest of the file off to the default serialiser.

This is a very quick example how you could store some key-value preferences for a number of users in a file.

using Newtonsoft.Json;

[JsonObject]
public class UserData {
    [JsonProperty] //Add a JSON property "Username" which is a string
    public string Username { get; set; }

    //IEnumerable types are converted to/from arrays automatically by Newtonsoft        
    [JsonProperty("Options")] //Set the name in JSON file to "Options"
    public Dictionary<string, string> Preferences { get; set; }

    [JsonIgnore] //Excludes this property from the JSON output
    public bool SaveRequired { get; set; } //Set true when a change is made, set false when saved
}

There are very similar libraries like this that do the exact same thing for XML, but I've not had much luck figuring them out and I'm usually on a very tight time scale when I just need something that I know works. If you can find a decent XML library and understand how to use it correctly, I'd recommend XML over JSON due to its strictly structured nature, and you can include schema versions and aid with integration into other systems by providing a well written schema.

Upvotes: 2

Related Questions