Problem w/ Simple Serialization

R

Ron M. Newman

Hi,

I have a simple class that has a Hashtable. the hashtable has a couple of
key/value pairs where the key is a string and the value is also a strong.

I have [Serializable] at the top of that class.

when I try to serialize it with XmlSerializer, I get only the "class
boundary" but my hashtable member doesn't get serialized. Hashtable is
Serializable, at least that's what the documentation says.

Am I missing something?

Thanks,
Ron
 
K

Kevin Spencer

There are 2 kinds of Serialization: "Binary" and XML. The [Serializable]
attribute relates to binary serialization, and HashTable cannot be
serialized as XML.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Orange you bland I stopped splaying bananas?
 
B

Barry Kelly

Ron M. Newman said:
I have a simple class that has a Hashtable. the hashtable has a couple of
key/value pairs where the key is a string and the value is also a strong.

I have [Serializable] at the top of that class.

when I try to serialize it with XmlSerializer, I get only the "class
boundary" but my hashtable member doesn't get serialized. Hashtable is
Serializable, at least that's what the documentation says.

Am I missing something?

Yup. Hashtable isn't serializable with XmlSerializer, and XmlSerializer
is a different thing than the [Serializable] attribute.

XmlSerializer deals with hierarchical object trees - it can't handle
cycles etc. It also only works with public read/write properties. It
works with the Xml* attributes in System.Xml.Serialization. You can
implement an interface to create custom serialization of your hashtable
data: IXmlSerializable.

[Serializable] is for use with the types in
System.Runtime.Serialization, and serialization formatters such as
BinaryFormatter and SoapFormatter. It works with private members, and
can handle arbitrary object graphs, so long as all the objects are
serializable.

-- Barry
 

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