如何在 .NET 中将自定义类型保存为二进制数据?
我正在创建一些有保存文件的东西。大多数情况下,它只是保存具有 4 个值的对象。
- x
- y
- id
- 层
我已经将其保存为 XML 一段时间了,问题是,文件开始变得巨大,因为我要保存数百到数千个这样的对象,而 XML 对于这样的东西来说有点庞大。我尝试切换到 Json,但它也太大了(不过我承认,更好)。
我的问题
我知道很多程序直接使用字节保存以节省空间。我想做这个。假设我有 300 个具有 X、Y、Id 和 Layer 属性的对象,如何将其作为字节保存到文件 x 并稍后加载?
当我曾经制作自己的服务器时,我尝试过读取字节。我通常最终会感到沮丧并放弃。我希望这不会太相似(我的直觉不这么认为)。
编辑
哦,对不起,大家,我忘了提及,我正在使用VB.NET
,所以所有.NET
答案都是可以接受的! :)
另外,所有这些值都是整数。
I'm creating something that has save files. For the most part, it's just saving objects with 4 values.
- x
- y
- id
- layer
I've been saving this as XML for a while, the problem is, the files are starting to get HUGE because I'm saving hundreds to thousands of these objects and XML is kind of bulky for something like this. I tried switching to Json but it was too big as well (I will admit though, better).
My Question
I know a lot of programs save directly using bytes to conserve space. I want to do this. Say I have 300 objects with the properties X, Y, Id, and Layer, how could I save this to file x as bytes and load it later?
I've tried reading bytes when I used to make my own servers. I usually ended up getting frustrated and giving up. I'm hoping this isn't too similar (my gut says otherwise).
EDIT
Oh sorry guys, I forgot to mention, I'm using VB.NET
so all .NET
answers are acceptable! :)
Also, all of these values are integers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会使用 protobuf-net (我会;我有偏见......)。它是一个开源 .NET 序列化程序,使用属性(嗯,这在 v2 中是可选的)来引导它。例如,使用 C# 语法(纯粹是为了我的方便 - 引擎不关心你使用什么语言):
然后你可以序列化,例如:
和反序列化:
工作完成;快速二进制输出。您可能可以通过在没有任何标记等的情况下手动编写所有读/写代码来获得更严格的一点,但是上面的维护工作会少得多。它还可以轻松加载到任何其他具有可用“协议缓冲区”实现的平台(即:大多数平台)。
I would use protobuf-net (I would; I'm biased...). It is an open-source .NET serializer, that uses attributes (well, that is optional in v2) to guide it. For example, using C# syntax (purely for my convenience - the engine doesn't care what language you use):
You can then serialize, for example:
and deserialize:
job done; fast binary output. You probably can get a little tighter by hand-coding all reading/writing manually without any markers etc, but the above will be a lot less maintenane. It'll also be easy to load into any other platform that has a "protocol buffers" implementation available (which is: most of them).
选择:
为什么不使用 xml 和 zip(或其他一些压缩)
优点:
* 它仍然是“人类”可读的
* 文本文件通常具有良好的压缩系数
另外您使用什么编程语言?
在 C/C++ 中或者您使用数据创建一个结构
重新解释将结构转换为 byte * 并将 sizeof 字节写入文件...就这么简单
(假设结构体是固定大小的(id和layer也是固定大小的变量,如int)
Alternative:
why not xml and zip (or some other compression)
Advantage:
* its still "human" readable
* text files have normaly a good compression factor
Also what programming languae you are using?
in C/C++ or you create a struct with the data
reinterpret cast the struct to byte * and write sizeof bytes to the file... simple as that
(assuming the struct is fixed size (id and layer are also fixed sized variables like int)
为什么不使用像 MySql 或 SQL 这样的数据库?
Why don´t you use a database like MySql or SQL ?