我已经从文本文件中读取了2 GB的long值,并将其读入VB.NET中的List(Of Long)。
因为将文本文件中的值读取到List(Of Long)中非常耗时,所以我想序列化List(Of Long),希望VB.NET为我提供一种快速反序列化的方法。
我该怎么办?
谢谢!
答案 0 :(得分:1)
这是一个使用XmlSerialization存储和检索数据的示例。在窗体上放置两个按钮(btnSerialize,btnDeserialize)和一个ProgressBar(ProgressBar1)。这在我的系统上产生了2.31 GB的文件。处理确实需要一段时间,因此异步代码和选取框进度条也是如此。
请注意,使用如此大的文件,它不会XmlSerializer.Deserialize()
一次反序列化整个过程(我遇到了“内存不足”的异常)。相反,我们必须逐条读取xml文件中的记录,这样可以提高内存效率:
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
Public Class Form1
Private FileName As String
Private HugeListOfLongs As New List(Of Long)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim MyDocs As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
FileName = System.IO.Path.Combine(MyDocs, "LongData.xml")
For L As Long = 0 To 100000000
HugeListOfLongs.Add(L)
Next
End Sub
Private Async Sub btnSerialize_Click(sender As Object, e As EventArgs) Handles btnSerialize.Click
btnSerialize.Enabled = False
btnDeserialize.Enabled = False
ProgressBar1.Style = ProgressBarStyle.Marquee
ProgressBar1.Visible = True
Dim success As Boolean
Await Task.Run(Sub()
Try
Dim xml As New XmlSerializer(HugeListOfLongs.GetType)
Using fs As New FileStream(FileName, FileMode.OpenOrCreate)
fs.SetLength(0) ' make sure it's empty before we start writing (so you don't have trailing data if new file is smaller than previous one)
xml.Serialize(fs, HugeListOfLongs)
End Using
success = True
Catch ex As Exception
MessageBox.Show("Failed to Serialize." & vbCrLf & vbCrLf & ex.ToString)
success = False
End Try
End Sub)
ProgressBar1.Visible = False
If success Then
MessageBox.Show("Data successfully written to: " & vbCrLf & FileName)
End If
btnSerialize.Enabled = True
btnDeserialize.Enabled = True
End Sub
Private Async Sub btnDeserialize_Click(sender As Object, e As EventArgs) Handles btnDeserialize.Click
btnSerialize.Enabled = False
btnDeserialize.Enabled = False
ProgressBar1.Style = ProgressBarStyle.Marquee
ProgressBar1.Visible = True
Dim success As Boolean
Await Task.Run(Sub()
HugeListOfLongs.Clear()
Try
Dim reader As XmlReader = XmlReader.Create(FileName)
While Not reader.EOF
If reader.Read Then
If reader.NodeType = XmlNodeType.Text Then
HugeListOfLongs.Add(Long.Parse(reader.Value))
End If
End If
End While
reader.Close()
success = True
Catch ex As Exception
MessageBox.Show("Failed to Deserialize." & vbCrLf & vbCrLf & ex.ToString)
success = False
End Try
End Sub)
ProgressBar1.Visible = False
If success Then
MessageBox.Show("Data successfully read from: " & vbCrLf & FileName & vbCrLf & vbCrLf & "Number of Longs: " & HugeListOfLongs.Count)
End If
btnSerialize.Enabled = True
btnDeserialize.Enabled = True
End Sub
End Class