Serialization woes

  • Thread starter Thread starter Ayende Rahien
  • Start date Start date
A

Ayende Rahien

I've a Hashtable in my app, that contain string as keys, and a class created
by xsd schema.
According to what I saw while googling Hashtable:
A>Absolutely impossible to serialize.
B>Possible, but with the correct datatype.

Anyway, it didn't work, so I turned to another solution, I copied all the
keys & values to two ArrayLists and tried to serialize them one after
another.
That didn't work too well either, because then XmlSerializer exceptioned
complaining about not knowing about my custom class statically.
Then I did this:

XmlSerializer xs = new XmlSerializer(typeof(String[]));
xs.Serialize(sw,(string[])KeyList.ToArray(typeof(string)));
XmlSerializer xs = new XmlSerializer(typeof(Custom[]));
xs.Serialize(sw,(Custom[])KeyList.ToArray(typeof(Custom)));

Which works without throwing exception, except that it doesn't generate
valid XML (there is a secod XML declaration).

What is the proper way to do this?
How do I serialize several objects into one stream, when they of different
types?
Is there a way to directly serialize the Hashtable, which would be the
optimal solution for me.
 
Hi,

You can Google for Aaron Sconnard's article on implementing IXmlSerializable
interface and serializing a dictionary class with XmlSerializer. I believe
it's somewhere on the MSDN site so you can try MSDN search too.
 
Dmitriy Lapshin said:
Hi,

You can Google for Aaron Sconnard's article on implementing IXmlSerializable
interface and serializing a dictionary class with XmlSerializer. I believe
it's somewhere on the MSDN site so you can try MSDN search too.

I found the article, but I would rather not use private stuff that may break
with the next release.
I want to stick to my plan to serialize two arraylists to the same stream,
one with values and one with keys.
The problem is that they keys & values are of different types, and
XmlSerializer choked on that.
 

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

Back
Top