简体   繁体   English

如何在.NET中将自定义类型保存为二进制数据?

[英]How can I save a custom type as binary data in .NET?

I'm creating something that has save files. 我正在创建一些保存文件的东西。 For the most part, it's just saving objects with 4 values. 在大多数情况下,它只是保存具有4个值的对象。

  • x X
  • y ÿ
  • id 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. 我一直把它保存为XML一段时间,问题是,文件开始变得巨大,因为我节省了数百到数千个这些对象,而XML对于这样的事情来说有点笨重。 I tried switching to Json but it was too big as well (I will admit though, better). 我尝试转换到Json,但它也太大了(我会承认,但更好)。

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? 假设我有300个具有属性X,Y,Id和Layer的对象,我怎么能将它作为字节保存到文件x并稍后加载?

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! 哦对不起伙计们,我忘了提,我正在使用VB.NET所以所有的.NET答案都是可以接受的! :) :)

Also, all of these values are integers. 此外,所有这些值都是整数。

I would use protobuf-net (I would; I'm biased...). 我会使用protobuf-net(我愿意;我有偏见......)。 It is an open-source .NET serializer, that uses attributes (well, that is optional in v2) to guide it. 它是一个开源的.NET序列化程序,它使用属性(嗯,在v2中是可选的)来指导它。 For example, using C# syntax (purely for my convenience - the engine doesn't care what language you use): 例如,使用C#语法(纯粹为了我的方便 - 引擎不关心你使用的语言):

[ProtoContract]
public class Whatever {
    [ProtoMember(1)]
    public int X {get;set;}
    [ProtoMember(2)]
    public int Y {get;set;}
    [ProtoMember(3)]
    public long Id {get;set;}
    [ProtoMember(4)]
    public string Layer {get;set;}
}

You can then serialize, for example: 然后,您可以序列化,例如:

List<Whatever> data = ...
Serializer.Serialize(file, data);

and deserialize: 和反序列化:

var data = Serializer.Deserialize<List<Whatever>>(file);

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). 它也很容易加载到任何其他具有“协议缓冲区”实现的平台(其中大部分都是)。

Alternative: why not xml and zip (or some other compression) 替代方案:为什么不xml和zip(或其他一些压缩)

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) 在C / C ++中或者你创建一个结构,数据重新解释将结构转换为byte *并将sizeof字节写入文件......简单(假设结构是固定大小的(id和layer也是固定大小的变量,如int) )

为什么不使用像MySql或SQL这样的数据库?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用C#,如何将二进制数据的字节数组转换为对数据建模的自定义类型对象? - With C#, how can I convert a byte array of binary data into a custom-typed object that models the data? 如何让.NET保存此图像? - How can I get .NET to save this image? 如何从URL中获取数据并将其保存到C#.NET中的二进制文件中,而不会出现编码问题? - How to GET data from an URL and save it into a file in binary in C#.NET without the encoding mess? 如何从注册表到字节数组读取二进制数据 - How can I read binary data from registry to byte array 如何在DataTable中使用SqlBulkCopy和二进制数据(byte [])? - How can I use SqlBulkCopy with binary data (byte[]) in a DataTable? 如何在 .NET MAUI 中保存临时文件? - How can I save temporary file in .NET MAUI? 使用.NET 3.5实体框架,如何保存实体? - Using the .NET 3.5 Entity Framework how can I save an entity? 当使用Full .net Framework序列化初始对象时,如何将二进制数据反序列化为.net Core中的对象? - How do I deserialize binary data into an object in .net Core, when the initial object was serialized with Full .net Framework? 在C#.NET中读取自定义二进制数据格式 - Reading custom binary data formats in C# .NET SQL <binary data> ASP.NET页面上显示的图像类型 - SQL <binary data> image type displayed on an ASP.NET page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM