如何在 vb.net 中序列化和反序列化字典?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18363727/
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
How can i serialize and deserialize a dictionary in vb.net?
提问by Daniel Valland
I am working with an application that needs to store large dictionaries of 40 000+ keys and values in a file, and then load them back into dictionaries on startup... Right now i am using a simple character separation with split and a for each loop on startup like:
key1=value1|key2=value2|key3=value3etc...
我正在使用一个应用程序,该应用程序需要在一个文件中存储 40 000 多个键和值的大型字典,然后在启动时将它们加载回字典中......现在我正在使用带有 split 和 a for each 的简单字符分隔启动时循环,如:
key1=value1|key2=value2|key3=value3等...
however, i am looking for a more efficient way of serializing and deserializing the dictionaries... also with size of the serialized data in mind as there is quite a lot of entries.
但是,我正在寻找一种更有效的方法来序列化和反序列化字典......还要考虑序列化数据的大小,因为有很多条目。
回答by Raphael Smit
You could make use of BinaryFormatter
On my mid end machine: save took: 390ms load took: 359ms data saved was about 1500kb
您可以使用BinaryFormatter
在我的中端机器上:保存时间:390 毫秒加载时间:359 毫秒保存的数据约为 1500kb
'save
Dim dict = New Dictionary(Of String, String)
For i = 1 To 40000
dict.Add("key" & i, "value" & i)
Next
Dim fs As IO.FileStream = New IO.FileStream("d:\test\test.bin", IO.FileMode.OpenOrCreate)
Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
bf.Serialize(fs, dict)
fs.Close()
'load
Dim fsRead As New IO.FileStream("d:\test\test.bin", IO.FileMode.Open)
Dim objTest As Dictionary(Of String, String) = bf.Deserialize(fsRead)
fsRead.Close()
回答by Fred Kerber
This works well when using stacked dictionaries:
这在使用堆叠字典时效果很好:
Dim DataDict as New Dictionary(Of String,Dictionary(Of String,String))
Here is a working example of embedded dictionaries being serialized for writing and reading:
这是一个嵌入式字典被序列化以进行读写的工作示例:
Imports System.IO
Module Modules
Public Sub TestDict()
Dim DictsToSave As New Dictionary(Of String, Dictionary(Of String, String))
For DictsToHave = 1 To 10
Dim SingelDictData As New Dictionary(Of String, String)
For Values = 1 To 10000
SingelDictData.Add("Key " & Values.ToString(), "Value " & Values.ToString())
Next
DictsToSave.Add("Key " & DictsToHave.ToString(), SingelDictData)
Next
Dim WriteResult = WriteMultiSerializedDict("D:\TestDict.Bin", DictsToSave)
Dim ReadResult As Dictionary(Of String, Dictionary(Of String, String)) = ReadMultiSerializedDict("D:\TestDict.Bin")
End Sub
Public Function WriteMultiSerializedDict(ByVal FullPath As String, ByVal DataDict As Dictionary(Of String, Dictionary(Of String, String))) As Boolean
Try
Dim FileStream As IO.FileStream = New FileStream(FullPath, IO.FileMode.OpenOrCreate)
Dim BinFormatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
BinFormatter.Serialize(FileStream, DataDict)
FileStream.Close()
Return True
Catch ex As Exception
Return False
End Try
End Function
Public Function ReadMultiSerializedDict(ByVal FullPath As String) As Dictionary(Of String, Dictionary(Of String, String))
Try
Dim DataDict As New Dictionary(Of String, Dictionary(Of String, String))
Dim FileStream As IO.FileStream = New FileStream(FullPath, IO.FileMode.Open)
Dim BinFormatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
DataDict = BinFormatter.Deserialize(FileStream)
FileStream.Close()
Return DataDict
Catch ex As Exception
Return Nothing
End Try
End Function
On my mid end dev machine, this took about 260 ms for the write and 545 for the read, with a file that measured at 3.70 mb. Important note: If you change any of the dict's keys (not values), the load bombs! So, as a matter of programming, always assemble your dicts in the same order. I've used this approach before, using 5 100 key/value pairs in the sub dictionary's, without issue or problem.
在我的中端开发机器上,写入需要大约 260 毫秒,读取需要 545 毫秒,文件大小为 3.70 mb。重要提示:如果你改变了字典的任何键(不是值),加载炸弹!因此,作为编程问题,请始终以相同的顺序组合您的 dicts。我以前使用过这种方法,在子字典中使用了 5 100 个键/值对,没有任何问题。

