save an arraylist in user.config

G

Guest

I am having a problem saving an object in the user.config file of my app (I
use VB and the my.settings namespace to do this FWIW).

The object that I am trying to save is an arraylist.

The arraylist itself holds multiple instances of a structure that I created.

The structure consists of one string, and four colours.

When I first added the arraylist to the settings tab of the "my project"
page, I had no trouble selecting system.collections.arraylist.

I've checked and found that both arraylist and colours should be
serializable. I added " <Serializable()> " to my structure as well.

I get no errors when trying to add an item to the arraylist
(my.settings.myarraylist.insert(0, oneStructure) ) and then saving it
(my.settings.save), but nothing seems to get saved.

If I look at the user.config file, I see the entry for my arraylist
(<setting name="messageCollection" serializeAs="Xml">), but the value tag is
either empty (<value />) or, sometimes mentions something about it holding an
array ("of any type", I think), along with some xml namespaces (which I
forgot to take note of). However, there is no real data in there.

Can anyone help?

thanks,
Doug
 
L

Lee Crabtree

Serializing a collection like an ArrayList goes a little something like
this:

using System.Xml.Serialization;

[XmlArray("Items"), XmlArrayItem("item", typeof(Item))]
public ArrayList items;

The XmlArray attribute lets the serializer know that the collection will
be serialized, and the XmlArrayItem attribute lets the serializer know
the type of object in the collection.

Once you have those, you can serialize to a file without any problem.

Lee Crabtree
 
G

Guest

Hi Lee,

Thanks for the pointers.

I feel a bit dumb, though, since I can't seem to make this work for me.

Could you provide a bit more detail?

My code has the following :

1. A structure declared something like this :
Public Structure MyStructure
dim message as string
dim color1 as color
dim color2 as color
End Structure

2. An arraylist to hold multiple instances of that structure.
Dim myArraylist as Arraylist

3. I am hoping to store the entire arraylist in the user.config file by
using code similar to this :
My.Settings.SavedArrayList = myArraylist

thanks again,
Doug

Lee Crabtree said:
Serializing a collection like an ArrayList goes a little something like
this:

using System.Xml.Serialization;

[XmlArray("Items"), XmlArrayItem("item", typeof(Item))]
public ArrayList items;

The XmlArray attribute lets the serializer know that the collection will
be serialized, and the XmlArrayItem attribute lets the serializer know
the type of object in the collection.

Once you have those, you can serialize to a file without any problem.

Lee Crabtree

Doug said:
I am having a problem saving an object in the user.config file of my app (I
use VB and the my.settings namespace to do this FWIW).

The object that I am trying to save is an arraylist.

The arraylist itself holds multiple instances of a structure that I created.

The structure consists of one string, and four colours.

When I first added the arraylist to the settings tab of the "my project"
page, I had no trouble selecting system.collections.arraylist.

I've checked and found that both arraylist and colours should be
serializable. I added " <Serializable()> " to my structure as well.

I get no errors when trying to add an item to the arraylist
(my.settings.myarraylist.insert(0, oneStructure) ) and then saving it
(my.settings.save), but nothing seems to get saved.

If I look at the user.config file, I see the entry for my arraylist
(<setting name="messageCollection" serializeAs="Xml">), but the value tag is
either empty (<value />) or, sometimes mentions something about it holding an
array ("of any type", I think), along with some xml namespaces (which I
forgot to take note of). However, there is no real data in there.

Can anyone help?

thanks,
Doug
 
G

Guest

I think I found a solution. (It works in my mini-test project anyway).

Here's how I got this to work.
1. I marked my structure as <serializable()>

2. In the user.config file, rather than adding an arraylist, I adda string
(which I can reference as My.Settings.MySerializedArrayList). However, I
still use an ArrayList within the program to hold my data.

3. When I go to save my ArrayList, I go through a few hoops to turned it
into a string representation. Something like this :

Dim bytearray(10000) As Byte
Dim stream As New MemoryStream(bytearray)
Dim formatter As New SoapFormatter()
formatter.Serialize(stream, myarraylist)
stream.Close()
My.Settings.MySerializedArrayList= Encoding.ASCII.GetString(bytearray)

(I create a MemoryString, using an array of bytes to initialize it. I use a
Soap formatters to create a serialized version of the arraylist, which is
stuffed into the memory stream, and therefore the byte array. I turn the byte
array into a string, and save it.)

4. When I retrieve the string, I go through a few more hoops to turn it back
into an arraylist. Like this :

Dim formatter As New SoapFormatter()
Dim bytearray() As Byte
bytearray = Encoding.ASCII.GetBytes(My.Settings.MySerializedArrayList)
formatter = New SoapFormatter()
Dim stream As New MemoryStream(bytearray)
myarraylist = CType(formatter.Deserialize(stream), ArrayList)
stream.Close()

(I read the string from User.Config and turn it into a byte array, which I
then use to initialize a memorystream. The memorystream is submitted to a
Soap [de]formatter and Ctyped back into an arraylist.)

I don't love this solution for a couple of reasons. One, it seems
unnecessarily complex. Two, when I save the arraylist, I had to specify an
initial size to my byte array, which means I will also have to find some way
to figure out how to determine the size that I should be using - or justuse
some rediculously high number and how that it never gets exceeded.
Doug Salomon said:
Hi Lee,

Thanks for the pointers.

I feel a bit dumb, though, since I can't seem to make this work for me.

Could you provide a bit more detail?

My code has the following :

1. A structure declared something like this :
Public Structure MyStructure
dim message as string
dim color1 as color
dim color2 as color
End Structure

2. An arraylist to hold multiple instances of that structure.
Dim myArraylist as Arraylist

3. I am hoping to store the entire arraylist in the user.config file by
using code similar to this :
My.Settings.SavedArrayList = myArraylist

thanks again,
Doug

Lee Crabtree said:
Serializing a collection like an ArrayList goes a little something like
this:

using System.Xml.Serialization;

[XmlArray("Items"), XmlArrayItem("item", typeof(Item))]
public ArrayList items;

The XmlArray attribute lets the serializer know that the collection will
be serialized, and the XmlArrayItem attribute lets the serializer know
the type of object in the collection.

Once you have those, you can serialize to a file without any problem.

Lee Crabtree

Doug said:
I am having a problem saving an object in the user.config file of my app (I
use VB and the my.settings namespace to do this FWIW).

The object that I am trying to save is an arraylist.

The arraylist itself holds multiple instances of a structure that I created.

The structure consists of one string, and four colours.

When I first added the arraylist to the settings tab of the "my project"
page, I had no trouble selecting system.collections.arraylist.

I've checked and found that both arraylist and colours should be
serializable. I added " <Serializable()> " to my structure as well.

I get no errors when trying to add an item to the arraylist
(my.settings.myarraylist.insert(0, oneStructure) ) and then saving it
(my.settings.save), but nothing seems to get saved.

If I look at the user.config file, I see the entry for my arraylist
(<setting name="messageCollection" serializeAs="Xml">), but the value tag is
either empty (<value />) or, sometimes mentions something about it holding an
array ("of any type", I think), along with some xml namespaces (which I
forgot to take note of). However, there is no real data in there.

Can anyone help?

thanks,
Doug
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top