C# 序列化数据表以供以后重用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2053142/
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-06 23:03:00  来源:igfitidea点击:

serializing a datatable for later reuse

c#.netxml-serializationdatatable

提问by larryq

I have a populated DataTable I'd like to serialize to a file for later use. I've been looking over the options involved with this and wondered if someone could point me in the right direction.

我有一个填充的 DataTable 我想序列化到一个文件以供以后使用。我一直在查看与此相关的选项,并想知道是否有人可以指出我正确的方向。

What I'll be creating are two methods-- one for writing the datatable to a file, and another for creating a new datatable using the file as input. Does it make sense to use the WriteXML() and Load() methods to do this, and if so, which flag(s) are ones to focus on? Thanks for the guidance.

我将创建的是两种方法——一种用于将数据表写入文件,另一种用于使用文件作为输入创建新的数据表。使用 WriteXML() 和 Load() 方法执行此操作是否有意义,如果是,那么需要关注哪些标志?感谢您的指导。

I'm using .Net 2.0 if that helps.

如果有帮助,我正在使用 .Net 2.0。

采纳答案by Muad'Dib

I would go for the read/write xml methods. We use that pretty extensively. It's quick, it's easy, it's built into the framework.

我会去读/写 xml 方法。我们非常广泛地使用它。它很快,很容易,它内置于框架中。

回答by paweloque

You might use the basic technique of serializing your database into CSV files with headers. Some database management systems support easy loading of data from such files. And in case your dbms doesn't it wouldn't be too difficult to write some code that'd do this for you. Does that answer your question?

您可以使用将数据库序列化为带有标题的 CSV 文件的基本技术。一些数据库管理系统支持从此类文件轻松加载数据。如果您的 dbms 没有,编写一些代码来为您执行此操作也不会太困难。这是否回答你的问题?

In my opinion the disadvantage of xml is that it contains possibly more meta-data than actual data. In case of csv files meta-data is not repeated.

在我看来,xml 的缺点是它可能包含比实际数据更多的元数据。在 csv 文件的情况下,元数据不会重复。

回答by Wagner Silveira

Is the datatable an object in memory? If so, you could simply go with Serialize and Deserialize Methods. They are relatively quickly and you can persist the result anywhere you want.

数据表是内存中的对象吗?如果是这样,您可以简单地使用序列化和反序列化方法。它们相对较快,您可以在任何您想要的地方保留结果。

回答by particle

I think Silveira commentmean use of binary serialization. And its right that it very fast compare to XML which serialization is very slow compare to binary specially for large amount of data. Also it take a lot less space on disk compare to XML.

我认为 Silveira评论意味着使用二进制序列化。与 XML 相比,它非常快是正确的,而 XML 与二进制相比,专门用于大量数据的序列化非常慢。与 XML 相比,它占用的磁盘空间也少得多。

    public static void Serialize(DataSet ds, Stream stream) {
        BinaryFormatter serializer = new BinaryFormatter();
        serializer.Serialize(stream, ds);
    }

    public static DataSet Deserialize(Stream stream) {
        BinaryFormatter serializer = new BinaryFormatter();
        return (DataSet)serializer.Deserialize(stream);
    } 

回答by Vaibhav.Inspired

IMPORTANT POINT :
If you try to serialize a DataTableobject or a DataSetobject using the binary formatter, you will still get a binary file, but it's a fairly large one because it's filled with a ton of XML data. Unfortunately, XML data in binary files makes for huge files that lack the portability and readability advantages that XML provides. Subsequently, deserializing such files might take seconds to complete and end up occupying much more memory than really needed. As a result, if you choose a binary serialization of ADO.NET objects because you need to get a more compact output, you will fail. The binary serialization is still the most space-effective approach, but with ADO.NET objects it doesn't prove to be as effective as it should.

重要点:
如果您尝试使用二进制格式化程序序列化一个DataTable对象或一个DataSet对象,您仍然会得到一个二进制文件,但它是一个相当大的文件,因为它充满了大量的 XML 数据。不幸的是,二进制文件中的 XML 数据会产生缺乏 XML 提供的可移植性和可读性优势的巨大文件。随后,反序列化此类文件可能需要几秒钟才能完成,并且最终会占用比实际需要更多的内存。因此,如果因为需要获得更紧凑的输出而选择 ADO.NET 对象的二进制序列化,则会失败。二进制序列化仍然是最节省空间的方法,但是对于 ADO.NET 对象,它并没有被证明是有效的。

For Complete reference read the following article:-
http://msdn.microsoft.com/en-us/magazine/cc188907.aspx

如需完整参考,请阅读以下文章:-
http://msdn.microsoft.com/en-us/magazine/cc188907.aspx