在 C# 中保存数据结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/370624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:51:52  来源:igfitidea点击:

Saving Data Structures in C#

c#serialization

提问by Pr?riewolf

I'm learning C# by writing a home library manager.

我正在通过编写家庭图书馆管理器来学习 C#。

I have a BookController that will store the books in a data structure and perform operations on them.

我有一个 BookController 将书籍存储在数据结构中并对它们执行操作。

Does C# have a way of saving the data in the dictionary to a local file perhaps in an XML fashion to load later, or am I going to have to write it myself?

C# 是否有办法将字典中的数据保存到本地文件中,也许以 XML 方式稍后加载,还是我必须自己编写?

What is the best method of saving and loading this data available to C#? Just need pointed in a good direction.

保存和加载可用于 C# 的数据的最佳方法是什么?只需要指出一个好的方向。

采纳答案by Marc Gravell

Actually, C# (the language) doesn't know anythingabout serialization, but .NET (the framework) provides lots of ways... XmlSerializer, BinaryFormatter, DataContractSerializer(.NET 3.0) - or there are a few bespoke serialization frameworks too.

事实上,C#(语言)不知道任何有关序列,但.NET(框架)提供了很多的方法... XmlSerializerBinaryFormatterDataContractSerializer(.NET 3.0) -或有一些定制的序列化框架了。

Which to use depends on your requirements; BinaryFormatteris simple to use, but burns assembly metadata information into the file - making it non-portable (you couldn't open it in Java, for example). XmlSerializerand DataContractSerializerare primarily xml-based, making it quite portable, but fairly large unless you compress it.

使用哪个取决于您的要求;BinaryFormatter使用简单,但将程序集元数据信息刻录到文件中 - 使其不可移植(例如,您无法在 Java 中打开它)。XmlSerializer并且DataContractSerializer主要是基于 xml 的,这使得它非常便携,但除非你压缩它,否则相当大。

Some proprietary serializers are half-way between the two; protobuf-netis a binary formatter (so very dense data), but which follows a portable standard for the data format (Google's protocol buffers spec). Whether this is useful depends on your scenario.

一些专有的序列化程序介于两者之间;protobuf-net是一个二进制格式化程序(非常密集的数据),但它遵循数据格式的可移植标准(Google 的协议缓冲区规范)。这是否有用取决于您的场景。

Typical code (here using XmlSerializer):

典型代码(此处使用XmlSerializer):

        XmlSerializer ser = new XmlSerializer(typeof(Foo));
        // write
        using (var stream = File.Create("foo.xml"))
        {
            ser.Serialize(stream, foo); // your instance
        }
        // read
        using (var stream = File.OpenRead("foo.xml"))
        {
            Foo newFoo = (Foo)ser.Deserialize(stream);
        }

回答by Ed S.

Look at Serializationand the ISerializable interface

查看SerializationISerializable 接口

It allows you to save an object's state in a portable form.

它允许您以可移植的形式保存对象的状态。

回答by Cameron MacFarland

If you want your data to be in XML format there's XmlSerializer.

如果您希望数据采用 XML 格式,则可以使用XmlSerializer

EDIT: For some reason I was downvoted for suggesting this because XmlSerializer cannot serialize a Dictionary.

编辑:出于某种原因,我因为 XmlSerializer 无法序列化字典而建议这样做而被否决。

To get around this problem you can always customize the serialization process like this

要解决此问题,您始终可以像这样自定义序列化过程

回答by gius

You should also consider looking at Linq to SQL (or Entity Framework) for O/R mapping and storing your data into a database (SQL CE, SQL Express).

您还应该考虑查看 Linq to SQL(或实体框架)以进行 O/R 映射并将数据存储到数据库(SQL CE、SQL Express)中。

回答by TWith2Sugars

I've created an extension method which takes an object and creates an xml string for you, all you need to do is save/load from file. XmlSerialisation

我创建了一个扩展方法,它接受一个对象并为您创建一个 xml 字符串,您需要做的就是从文件中保存/加载。 Xml序列化