Click to See Complete Forum and Search --> : Saving array/arraylist to file and retrieving


Franco22
07-17-2006, 01:52 AM
Is there a simple way of saving an array or an arraylist to a file and retrieving the info in VB.Net? I'm thinking of using XML serialisation but it seems a bit complicated.

Phil Weber
08-01-2006, 12:41 AM
XML serialization requires the least code:

' Imports System.IO
' Imports System.Collections
' Imports System.Xml.Serialization

Dim list As New ArrayList()
With list
.Add("Item 1")
.Add("Item 2")
.Add("Item 3")
.Add("Item 4")
.Add("Item 5")
End With

Dim stmFile As New FileStream("d:\path\file.xml", FileMode.Create)
Dim xtr As New Xml.XmlTextWriter(stmFile, Nothing)
Dim xml As New XmlSerializer(list.GetType)
xml.Serialize(xtr, list)
stmFile.Close()

The only other method I know is to loop through the list and manually write each item to the file.