Serializing an XML Hash Table (C # 3.0)

Hi, I am trying to serialize a hash table but not happening
private void Form1_Load(object sender, EventArgs e) { Hashtable ht = new Hashtable(); DateTime dt = DateTime.Now; for (int i = 0; i < 10; i++) ht.Add(dt.AddDays(i), i); SerializeToXmlAsFile(typeof(Hashtable), ht); } private void SerializeToXmlAsFile(Type targetType, Object targetObject) { try { string fileName = @"C:\output.xml"; //Serialize to XML XmlSerializer s = new XmlSerializer(targetType); TextWriter w = new StreamWriter(fileName); s.Serialize(w, targetObject); w.Flush(); w.Close(); } catch (Exception ex) { throw ex; } } 

After searching on Google, I found that objects that cannot be initialized cannot be serialized. However, I have been successful in binary serialization.

But I want to have an xml one. Is there any way to do this?

I am using C # 3.0

thanks

+3
source share
3 answers

First of all, starting with C # 2.0, you can use a secure version like the very old Hashtable that ships with .NET 1.0. That way you can use Dictionary<DateTime, int> .

Starting with .NET 3.0, you can use the DataContractSerializer . This way you can rewrite the code as shown below.

 private void Form1_Load(object sender, EventArgs e) { MyHashtable ht = new MyHashtable(); DateTime dt = DateTime.Now; for (int i = 0; i < 10; i++) ht.Add(dt.AddDays(i), i); SerializeToXmlAsFile(typeof(Hashtable), ht); } 

where SerializeToXmlAsFile and MyHashtable type that you define as follows:

 [CollectionDataContract (Name = "AllMyHashtable", ItemName = "MyEntry", KeyName = "MyDate", ValueName = "MyValue")] public class MyHashtable : Dictionary<DateTime, int> { } private void SerializeToXmlAsFile(Type targetType, Object targetObject) { try { string fileName = @"C:\output.xml"; DataContractSerializer s = new DataContractSerializer (targetType); XmlWriterSettings settings = new XmlWriterSettings (); settings.Indent = true; settings.IndentChars = (" "); using (XmlWriter w = XmlWriter.Create (fileName, settings)) { s.WriteObject (w, targetObject); w.Flush (); } } catch (Exception ex) { throw ex; } } 

This code creates a C: \ output.xml file with the following:

 <?xml version="1.0" encoding="utf-8"?> <AllMyHashtable xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/DataContractXmlSerializer"> <MyEntry> <MyDate>2010-06-09T22:30:00.9474539+02:00</MyDate> <MyValue>0</MyValue> </MyEntry> <MyEntry> <MyDate>2010-06-10T22:30:00.9474539+02:00</MyDate> <MyValue>1</MyValue> </MyEntry> <!-- ... --> </AllMyHashtable> 

So, as we can see all the element names of the XML destination files that we can freely define.

+3
source

You can create your own Hashtable derived from the standard Hashtable, with the implementation of IXmlSerializable. This way you will implement ReadXml (XmlReader reader) and WriteXml (XmlWriter writer), where you can put your own logic on how to read and write values ​​from your Hashtablw with XmlReader and XmlWriter data.

+1
source

I suggest you use DataContractSerializer, more powerful and easy to use.

0
source

All Articles