December 2005 - Posts

XMLSerializable Hashtable

Well its high time that the hash table became XML serializable but i still havent figured out  why not.
But while I was thinking about this just thought i'd put it down myself.

I would like to know if there is a more standard implementation for this. But I just managed to this chewing gum class done for now.

using System;
using System.Runtime.Serialization;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;

namespace XMLSerializableHashTable
{
	[Serializable]
	public class SerializableHashtable:Hashtable,
				System.Xml.Serialization.IXmlSerializable
	{
		
		#region IXmlSerializable Members

		#region Node class
		/// 
		/// This class would be for custom serialization
		/// 
		[Serializable]
		public class Node
		{
			public Node()
			{}

			public Node(string k,object v)
			{
				key = k;
				val = v;
			}

			public string key;
			public object val;
		}

		#endregion Node class for XML Serialization

		/// 
		/// Write the xml using an array list
		/// using the Node to store key value pairs
		/// 
		/// 
		public void WriteXml(System.Xml.XmlWriter writer)
		{
			XmlSerializer xs = new XmlSerializer(typeof(System.Collections.ArrayList),
												new System.Type[]{typeof(Node)});
			ArrayList list = new ArrayList();
			foreach(string key in this.Keys)
			{
				list.Add(new Node(key,this[key]));
			}
			xs.Serialize(writer,list);
		}

		public System.Xml.Schema.XmlSchema GetSchema()
		{
			// TODO:  Add SerializableHashtable.GetSchema implementation
			return null;
		}

		/// 
		/// Deserialization using array list
		/// and the node(key,value) pairs
		/// 
		/// 
		public void ReadXml(System.Xml.XmlReader reader)
		{
			XmlSerializer xs = new XmlSerializer(typeof(System.Collections.ArrayList),
												new System.Type[]{typeof(Node)});

			//Move the reader into the ArrayList element.
			reader.Read();
			ArrayList list  = xs.Deserialize(reader) as ArrayList;
			Node node;

			if(list == null)
				return;

			//Reload the hashTable.
			for(int i=0;ithis.Add(node.key,node.val);
			}
		}
		#endregion
	}
}
with 2 Comments

Quest for the value of the Output parameter - I

Lets hit the specs for the output paramter

·         A variable need not be definitely assigned before it can be passed as an output parameter in a function member invocation.

·         Following the normal completion of a function member invocation, each variable that was passed as an output parameter is considered assigned in that execution path.

·         Within a function member, an output parameter is considered initially unassigned.

·         Every output parameter of a function member must be definitely assigned (§ 5.3) before the function member returns normally.

How do we get the value then ?

The point that totally put me of was the debugger that had the value. Why can I not access it :) ?