Вот пример использования XmlSerialization для хранения и извлечения данных. Удалите две кнопки (btnSerialize, btnDeserialize) и индикатор ProgressBar (ProgressBar1) на форме. В результате в моей системе был файл размером 2,31 ГБ. Обработка занимает некоторое время, таким образом, асинхронный код и маркер прогрессбар.
Обратите внимание, что при таком большом файле он не будет десериализовать все это за один снимок с 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