I f i had a simple class with just a few propertys, What would be the best approach to saving that (List Of (class_name)) to a textfile and retrieving it aswell ?
Thanks.
Printable View
I f i had a simple class with just a few propertys, What would be the best approach to saving that (List Of (class_name)) to a textfile and retrieving it aswell ?
Thanks.
Serialization would be the solution. XML or Binary serialization, whichever you prefer.
Read this.
I would suggest binary serialization in that case, since it is very easy to implement and provides some safety from casual editing of the file. If you actually want to be able to edit the file by hand, then XML is the way to go.
Thanks for the replys. Ill have a look at them.
I think I described how to do binary serialization within the last month or two, so you could search this forum on that info. However, once you have the filename, binary serialization takes two lines, and one of them may not be entirely necessary. It's really that simple.
Ok, i decided to do a test with both just for future reference. With the binary serialization i tried this
It gave me an error about not be serializable so i set the attribute in the class but i have two questions.Code:Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim myWriter As New IO.FileStream("C:\myFileName.bin", IO.FileMode.Create)
'P is an instance of my class
formatter.Serialize(myWriter, P)
myWriter.Close()
1. Is that code ok for doing binary serialization ?
2. By setting the <Serializable()> attribute, does it do only that to the class, by that i mean, if it was set by default, you wouldnt change any code at all when creating classes ?
Thanks.
1. Yes that code is perfect:)
2. How do you mean? You just need to make sure everything inside the class is serializable. All classes and structures should be marked with the serializable attribute.
What do you mean by that ?, i just declared it at the top of the class.Quote:
You just need to make sure everything inside the class is serializable
Is there more i need to do ?Code:<Serializable()> Public Class Person
'all propertys
End Class
Plus, while im typing the reply, i might aswell ask. Is there much difference if i needed to save a (List Of Person) ?
Thank you for your replys.
What I ment was, if your class contains a structure for example, it too needs to be marked as serializable.
And you need to make sure that every class you're using inside the class is serializable. If they arent, you'll notice:)
The List(Of T) is serializable so there wont be any problem serializing a List(Of Person) as long as Person is serializable.
Right, i understand.
Thank you for your help.
I have been trying to add a list of person and retrieve it back but i cant get my head around it.
This only seems to add the last item of the list.
How do we read the list back aswell ?Code:Dim L As New List(Of Person)
Dim P As New Person
For i As Integer = 1 To 5
P.Forename = "Firstname" & i.ToString
P.Surname = "Surname" & i.ToString
L.Add(P)
Next
Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim myWriter As New IO.FileStream("C:\myFileName.bin", IO.FileMode.Create)
formatter.Serialize(myWriter, L)
myWriter.Close()
Thank you.
You use the Deserialize method. Its return type is Object so you need to cast it to List(Of Person).
VB.Net Code:
Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter Dim fs As New IO.FileStream("C:\myFileName.bin", IO.FileMode.Open) L = CType(formatter.Deserialize(fs), Global.System.Collections.Generic.List(Of Person)) fs.Close()
That looks perfect for retrieving but how about the code i have for saving, the retrieving part shows that i am saving 5 items with L.Count.ToString but every item from L(0).Forename to L(4).Forename shows the same thing(the last item added to the list.
Is it something to do with this line
formatter.Serialize(myWriter, L)
Thank you for your patience Atheist
Sorry, i needed to declare a new person inside the loop.
Thank you.
It might be your loop, you're setting the Forename and Surname property of the same Person instance everytime. Change the loop to something like this:
vb Code:
Dim P As Person For i As Integer = 1 To 5 P = New Person P.Forename = "Firstname" & i.ToString P.Surname = "Surname" & i.ToString L.Add(P) Next
Edit: Oh should've refreshed before replying :)
:) You have been a great help, now to saving and retrieving list of person using XmlSerializer :bigyello:
Thanks Atheist
Just out of curiosity, why are you doing both?
When i read that i decided to create a test project for future use so i wanted to learn both methods.Quote:
Originally Posted by Shaggy Hiker
I have just read that you cant use an arraylist or a listof type for xml, do you have to convert to an array ?
Thank you.
Where did you read that? You can serialize a List(Of T) using XML serialization, though I'm not so sure about the ArrayList..But dont worry about that, because you shouldnt be using the ArrayList in any version of .Net higher than 1.1.Quote:
Originally Posted by user name
I tried the same as the binary method but using xml but i get an error.
There was an error generating the XML document.
ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/fxref_system.xml/html/92d10c1b-a2dd-1911-fc1c-b790b994b5af.htmCode:Dim L As New List(Of Person)
For i As Integer = 1 To 5
Dim P As New Person
P.Forename = "forename" & i.ToString
P.Surname = "surname" & i.ToString
L.Add(P)
Next
Dim mySerializer As New Xml.Serialization.XmlSerializer(GetType(Person))
Dim myWriter As New IO.FileStream("C:\myFileName.xml", IO.FileMode.Create)
mySerializer.Serialize(myWriter, L)
myWriter.Close()
Not sure if i am reading that correctly but it does say under Note.
The XmlSerializer cannot serialize the following: arrays of ArrayList and arrays of List<(Of <(T>)>).
Thank you.
Take a look at what youre passing to the XMLSerializers constructor:
VB.Net Code:
Dim mySerializer As New Xml.Serialization.XmlSerializer(GetType(Person))
You're passing GetType(Person), but you're trying to serialize a List(Of Person).
Oh Man, I deserve that through being lazy, copying code and just changing a couple of things from the single save and obviously missing that.
Good find Atheist, you are the man :thumb:
Thank you very much.