在 .NET (C#) 中本地存储数据的最佳方式 [英] Best way to store data locally in .NET (C#)

查看:69
本文介绍了在 .NET (C#) 中本地存储数据的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,它获取用户数据并将其存储在本地以备后用.应用程序将经常启动和停止,我想让它在应用程序启动/结束时保存/加载数据.

I'm writing an application that takes user data and stores it locally for use later. The application will be started and stopped fairly often, and I'd like to make it save/load the data on application start/end.

如果我使用平面文件会相当简单,因为数据并不真正需要保护(它只会存储在这台 PC 上).我相信的选项是:

It'd be fairly straightforward if I used flat files, as the data doesn't really need to be secured (it'll only be stored on this PC). The options I believe are thus:

  • 平面文件
  • XML
  • SQL 数据库

平面文件需要更多的维护工作(没有像 XML 那样的内置类),但是我以前没有使用过 XML,对于这个相对简单的任务,SQL 似乎有点过分.

Flat files require a bit more effort to maintain (no built in classes like with XML), however I haven't used XML before, and SQL seems like overkill for this relatively easy task.

还有其他值得探索的途径吗?如果不是,以下哪个是最佳解决方案?

Are there any other avenues worth exploring? If not, which of these is the best solution?

要向问题添加更多数据,基本上我唯一想存储的是一个看起来像这样的字典

To add a little more data to the problem, basically the only thing I'd like to store is a Dictionary that looks like this

Dictionary<string, List<Account>> 

其中 Account 是另一种自定义类型.

where Account is another custom type.

我是否会将 dict 序列化为 xmlroot,然后将 Account 类型序列化为属性?

Would I serialize the dict as the xmlroot, and then the Account type as attributes?

更新 2:

因此可以序列化字典.让它变得复杂的是,这个 dict 的值本身就是一个泛型,它是一个类型为 Account 的复杂数据结构的列表.每个 Account 都相当简单,只是一堆属性.

So it's possible to serialize a dictionary. What makes it complicated is that the value for this dict is a generic itself, which is a list of complex data structures of type Account. Each Account is fairly simple, it's just a bunch of properties.

据我所知,这里的目标是尝试并以此结束:

It is my understanding that the goal here is to try and end up with this:

<Username1>
    <Account1>
        <Data1>data1</Data1>
        <Data2>data2</Data2>
    </Account1>
</Username1>
<Username2>
    <Account1>
        <Data1>data1</Data1>
        <Data2>data2</Data2>
    </Account1>
    <Account2>
        <Data1>data1</Data1>
        <Data2>data2</Data2>
    </Account2>
 </Username2>

正如你所见,继承人是

  • 用户名(字典字符串)>
  • 帐户(列表中的每个帐户)>
  • 帐户数据(即类属性).

Dictionary> 获取这个布局是一个棘手的问题,也是这个问题的本质.

Obtaining this layout from a Dictionary<Username, List<Account>> is the tricky bit, and the essence of this question.

这里有很多关于序列化的如何做"的回答,这是我的错,因为我没有早点说清楚,但现在我正在寻找一个明确的解决方案.

There are plenty of 'how to' responses here on serialisation, which is my fault since I didn't make it clearer early on, but now I'm looking for a definite solution.

推荐答案

我会将文件存储为 JSON.由于您存储的字典只是一个名称/值对列表,因此这几乎就是 json 的设计目的.
有很多不错的免费 .NET json 库 - 这里是 一个,但你可以在第一个链接上找到完整列表.

I'd store the file as JSON. Since you're storing a dictionary which is just a name/value pair list then this is pretty much what json was designed for.
There a quite a few decent, free .NET json libraries - here's one but you can find a full list on the first link.

这篇关于在 .NET (C#) 中本地存储数据的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆