NameValueCollection Serialization Deserialization

G

Guest

We are trying to serialize the Request.ServerVariables collection
(NameValueCollection) to an XML formatted string, to insert into a database.
The problem we are running into is that most of the example that we find
discuss writing to a file. Does anyone have a simple function to do this?

We would also like to deserialize the string from the database back into a
NameValueCollection.
 
K

Karl Seguin

It's sad, but you can't use the XmlSerializer for this. the only easy/good
alternative I know of is to use the SoapFormatter:

NameValueCollection values = new NameValueCollection();
values.Add("one", "two");
values.Add("a", "b");

SoapFormatter ser = new SoapFormatter();
MemoryStream ms = new MemoryStream();
ser.Serialize(ms, values);
byte[] b = ms.GetBuffer();
string s = Encoding.Default.GetString(b);
ms.Close();

you'll need to add a reference to
System.Runtime.Serialization.Formatters.Soap.dll

Karl
 
G

Guest

Thanks Karl! This will help tremendously. Can this string be deserialized
back into a NameValueCollection as well?

Karl Seguin said:
It's sad, but you can't use the XmlSerializer for this. the only easy/good
alternative I know of is to use the SoapFormatter:

NameValueCollection values = new NameValueCollection();
values.Add("one", "two");
values.Add("a", "b");

SoapFormatter ser = new SoapFormatter();
MemoryStream ms = new MemoryStream();
ser.Serialize(ms, values);
byte[] b = ms.GetBuffer();
string s = Encoding.Default.GetString(b);
ms.Close();

you'll need to add a reference to
System.Runtime.Serialization.Formatters.Soap.dll

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Mike Logan said:
We are trying to serialize the Request.ServerVariables collection
(NameValueCollection) to an XML formatted string, to insert into a database.
The problem we are running into is that most of the example that we find
discuss writing to a file. Does anyone have a simple function to do this?

We would also like to deserialize the string from the database back into a
NameValueCollection.
 
K

Karl Seguin

Yes, if you pull up the documentation for SoapFormatter you'll see....but,
as you probably could have guessed, there's a Deserialize function ;)

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Mike Logan said:
Thanks Karl! This will help tremendously. Can this string be deserialized
back into a NameValueCollection as well?

Karl Seguin said:
It's sad, but you can't use the XmlSerializer for this. the only easy/good
alternative I know of is to use the SoapFormatter:

NameValueCollection values = new NameValueCollection();
values.Add("one", "two");
values.Add("a", "b");

SoapFormatter ser = new SoapFormatter();
MemoryStream ms = new MemoryStream();
ser.Serialize(ms, values);
byte[] b = ms.GetBuffer();
string s = Encoding.Default.GetString(b);
ms.Close();

you'll need to add a reference to
System.Runtime.Serialization.Formatters.Soap.dll

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Mike Logan said:
We are trying to serialize the Request.ServerVariables collection
(NameValueCollection) to an XML formatted string, to insert into a database.
The problem we are running into is that most of the example that we find
discuss writing to a file. Does anyone have a simple function to do this?

We would also like to deserialize the string from the database back into a
NameValueCollection.
 

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