Serializing into an XML file a Hashtable

V

Victor Paraschiv

I need to serialize into an XML file a hashtable.
From MSDN at XmlSerializer:
http://msdn.microsoft.com/library/d...emxmlserializationxmlserializerclasstopic.asp

I implemented the indexable property and the add function but I'm still
getting the following exception:"The type System.Collections.Hashtable
is not supported because it implements IDictionary."


My code for Item and Add add-on looks like this:
Code:
[Serializable]
public class DaysArchive
{

public class HashTableItems
{
private IDictionaryEnumerator HashEntries;
public HashTableItems()
{
}

public  void Initialize(Hashtable table)
{
HashEntries = table.GetEnumerator();
}

public System.Collections.DictionaryEntry this[string key]
{
get
{
HashEntries.Reset();
HashEntries.MoveNext();
while ((string)HashEntries.Key != key)
{
HashEntries.MoveNext();
}
return new DictionaryEntry(HashEntries.Key,HashEntries.Value);
}
}
}

public readonly HashTableItems Item;
public Hashtable archive;

public DaysArchive()
{
archive = new Hashtable();
this.Item = new HashTableItems();
this.Item.Initialize(archive);
}

public void AddItem(string Name, bool offline)
{
if (archive.ContainsKey(Name) == false)
archive.Add(Name,offline);
}

public bool GetKeyValue(string Name)
{
if (archive.ContainsKey(Name) == false)
throw new Exception("Key could not be found!");

IDictionaryEnumerator days_enum = archive.GetEnumerator();


days_enum.MoveNext();
while (((string)days_enum.Key != Name)&&(days_enum.MoveNext()));

return (bool)days_enum.Value;
}

public bool IsKeyInCollection(string Name)
{
return archive.ContainsKey(Name);
}

public void Add(System.Collections.DictionaryEntry cel)
{
archive.Add(cel.Key,cel.Value);
}
}
 
B

bart_deboeck

Hi Victor,

I am probably missing something, what do you use HashTableItems for ?
What is the role of this part of the code :

HashEntries.Reset();
HashEntries.MoveNext();
while ((string)HashEntries.Key != key)
{
HashEntries.MoveNext();
}

Are you trying to check the existence of a key ? Such type of
functionality is implemented by a Hashtable.

What is the role of DaysArchive ? Do you want to store (Name,
offline) pairs ? A Hashtable should be sufficient for this type of
functionality.
Please feel free to contact me if you need any more information.

Regards,
Bart

http://www.xenopz.com/blog/bartdeboeck/
 
V

Victor Paraschiv

Hi Bart,

HashTableItems is a custom made class which I use to define the suport
for the Item indexed property. I make an instance of this class and I
return it in the get method of the Item property.

My goal is to serialize the Hashtable into an XML file. I you try to
serialize a hashtable the XmlSerializer throws an exception that says
Hashtable impelements IDictionary. Now I wrote my own class which has
an internal Hashtable. In the hash I store a string wich is the name of
a file and a boolean variable which represents the state of the file
data (if it was obtain from a the realtime-market or tha data was
aquired when the market was offline)
I modiffied the implementation of the whole DaysArchive class as follows
but I still get an exception that says "You must implement the default
accessor for DaysArchive because it inherits ICollection"
I still have not a clue what am I supposed to do next.

Thank you very much for your support.


Code:
public class DaysArchive:ICollection,IEnumerable
{
private Hashtable archive;
private HashItems hashitems;

public DaysArchive()
{
archive  = new Hashtable();
hashitems = new HashItems();
hashitems.Refresh( archive);
}

#region implementations for ICollection & IEnumerable

public int Count
{
get
{
return archive.Count;
}
}


public bool IsSynchronized
{
get
{
return archive.IsSynchronized;
}
}


public object SyncRoot
{
get
{
return archive.SyncRoot;
}
}


public void CopyTo(	Array array,int index)
{
archive.CopyTo(array,index);
}


public IEnumerator GetEnumerator()
{
return archive.GetEnumerator();
}


#endregion

public bool ContainsKey(string Name)
{
return archive.ContainsKey(Name);
}


public void Add(DictionaryEntry cel)
{
archive.Add(cel.Key,cel.Value);
hashitems.Refresh(archive);
}


public void Add(object key, object value)
{
archive.Add(key,value);
hashitems.Refresh( archive);
}


public class HashItems
{
Hashtable hash;

public HashItems(Hashtable hash)
{
this.hash = hash;
}
public HashItems()
{
}

public DictionaryEntry this[int index]
{
get
{
IDictionaryEnumerator enumerator = hash.GetEnumerator();

enumerator.MoveNext();

while(index>0)
{
enumerator.MoveNext();
index--;
}
return new DictionaryEntry(enumerator.Key,enumerator.Value);
}
}

public void Refresh( Hashtable hash)
{
this.hash = hash;
}

public int Count
{
get
{
return hash.Count;
}
}
}


public HashItems Item
{
get
{
return hashitems;
}
}
}
 
B

bart_deboeck

Hi Victor,

Thanks for the extra info. I'll take a look at your question with the
extra info tomorrow.

cu,
Bart

http://www.xenopz.com/blog/bartdeboeck/

Victor Paraschiv schreef:
Hi Bart,

HashTableItems is a custom made class which I use to define the suport
for the Item indexed property. I make an instance of this class and I
return it in the get method of the Item property.

My goal is to serialize the Hashtable into an XML file. I you try to
serialize a hashtable the XmlSerializer throws an exception that says
Hashtable impelements IDictionary. Now I wrote my own class which has
an internal Hashtable. In the hash I store a string wich is the name of
a file and a boolean variable which represents the state of the file
data (if it was obtain from a the realtime-market or tha data was
aquired when the market was offline)
I modiffied the implementation of the whole DaysArchive class as follows
but I still get an exception that says "You must implement the default
accessor for DaysArchive because it inherits ICollection"
I still have not a clue what am I supposed to do next.

Thank you very much for your support.


Code:
	public class DaysArchive:ICollection,IEnumerable
	{
		private Hashtable archive;
		private HashItems hashitems;

		public DaysArchive()
		{
			archive  = new Hashtable();
			hashitems = new HashItems();
			hashitems.Refresh( archive);
		}

		#region implementations for ICollection & IEnumerable

		public int Count
		{
			get
			{
				return archive.Count;
			}
		}


		public bool IsSynchronized
		{
			get
			{
				return archive.IsSynchronized;
			}
		}


		public object SyncRoot
		{
			get
			{
				return archive.SyncRoot;
			}
		}


		public void CopyTo(	Array array,int index)
		{
			archive.CopyTo(array,index);
		}


		public IEnumerator GetEnumerator()
		{
			return archive.GetEnumerator();
		}


		#endregion

		public bool ContainsKey(string Name)
		{
			return archive.ContainsKey(Name);
		}


		public void Add(DictionaryEntry cel)
		{
			archive.Add(cel.Key,cel.Value);
			hashitems.Refresh(archive);
		}


		public void Add(object key, object value)
		{
			archive.Add(key,value);
			hashitems.Refresh( archive);
		}


		public class HashItems
		{
			Hashtable hash;

			public HashItems(Hashtable hash)
			{
				this.hash = hash;
			}
			public HashItems()
			{
			}

			public DictionaryEntry this[int index]
			{
				get
				{
					IDictionaryEnumerator enumerator = hash.GetEnumerator();

					enumerator.MoveNext();

					while(index>0)
					{
						enumerator.MoveNext();
						index--;
					}
					return new DictionaryEntry(enumerator.Key,enumerator.Value);
				}
			}

			public void Refresh( Hashtable hash)
			{
				this.hash = hash;
			}

			public int Count
			{
				get
				{
					return hash.Count;
				}
			}
		}


		public HashItems Item
		{
			get
			{
				return hashitems;
			}
		}
	}
 
V

Victor Paraschiv

Problem solved. I discarded everything was written in MSDN library at
XmlSerializer.
I've built a custom class that contains internally a Hashtable and
inherits ICollection and IEnumerable. I added then an indexer which
returns the hashtableitem at the given index. That's all!

Victor Paraschiv
 

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