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

查看:2109
本文介绍了将数据存储在本地的.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.

这将会是相当简单的,如果我用纯文本文件,因为该数据并不真正需要保护(这将只存储在此计算机上)。我相信,这些选项是这样的:

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>>

其中帐户是另一种自定义类型。

where Account is another custom type.

我会序列化字典作为xmlroot,然后将账户类型属性?

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


更新2:

所以这是可能的序列化的字典。是什么使得它复杂的是,对这个字典的值是一个通用的本身,它是类型账户的复杂的数据结构的列表。每个账户非常简单,它只是一堆属性。

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>

正如你所看到的heirachy是

As you can see the heirachy is

  • 在用户名(字典的字符串)>
  • 帐户(列表中的每个帐户)>
  • 帐户数据(也就是类属性)。

获得此布局的词典&lt;用户名,名单,其中,账户&GT;&GT; 是有点棘手,而这个问题的实质

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 。既然你存储的字典这仅仅是一个名称/值对列表,那么这是pretty的很多东西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天全站免登陆