471,350 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

System.Collections.Hashtable serializable to xml

is it possible to make a universal hashtable that would be able to serialize itself to xml format?
universal one, meaning I don't have to know about the type of keys and values in advance.

Ivan
Dec 14 '05 #1
1 5323
here is a somewhat working solution (for reference http://www.mattberther.com/2004/06/000487.html)

using System;
using System.Xml;
using System.IO;
using System.Xml.Serialization;
using System.Collections;

namespace Utils
{
public class XmlSerializableHashtable : IXmlSerializable, IEnumerable
{
private Hashtable _table;

public XmlSerializableHashtable()
{
this._table = new Hashtable();
}

private XmlSerializableHashtable(Hashtable table)
{
this._table = table;
}

void IXmlSerializable.ReadXml(XmlReader reader)
{
reader.Read();
while (reader.NodeType != XmlNodeType.EndElement)
{
reader.ReadStartElement("item");
string key = reader.ReadElementString("key");
string value = reader.ReadElementString("value");
reader.ReadEndElement();
reader.MoveToContent();
_table.Add(key, value);
}
}

void IXmlSerializable.WriteXml(XmlWriter writer)
{
foreach (object key in _table.Keys)
{
object value = _table[key];
writer.WriteStartElement("item");
writer.WriteElementString("key", key.ToString());
writer.WriteElementString("value", value.ToString());
writer.WriteEndElement();
}
}

System.Xml.Schema.XmlSchema IXmlSerializable.GetSchema()
{
return null;
}

public void Add(object key, object value)
{
_table.Add(key, value);
}

public void Remove(object key)
{
_table.Remove(key);
}

public void Clear()
{
_table.Clear();
}

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

public object this[object key]
{
get { return _table[key]; }
set { _table[key] = value; }
}

public ICollection Keys
{
get { return _table.Keys; }
}

public ICollection Values
{
get { return _table.Values; }
}

public bool ContainsKey(object key)
{
return _table.ContainsKey(key);
}

public bool ContainsValue(object key)
{
return _table.ContainsValue(key);
}

public bool Contains(object key)
{
return _table.Contains(key);
}

/// <summary> returns a shallow copy of the XmlSerializableHashtable</summary>
public object Clone()
{
return new XmlSerializableHashtable(_table.Clone() as Hashtable);
}

public IEnumerator GetEnumerator()
{
return new Enumerator(_table.GetEnumerator());
}

private class Enumerator : IEnumerator
{
private IEnumerator inner = null;

public Enumerator(IEnumerator inner)
{
this.inner = inner;
}

public void Reset()
{
inner.Reset();
}

public object Current
{
get { return ((DictionaryEntry) inner.Current).Value; }
}

public bool MoveNext()
{
return inner.MoveNext();
}
}
}

}

Ivan wrote:
is it possible to make a universal hashtable that would be able to
serialize itself to xml format?
universal one, meaning I don't have to know about the type of keys and
values in advance.

Ivan

Dec 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Chua Wen Ching | last post: by
3 posts views Thread by nzanella | last post: by
3 posts views Thread by MioTheGreat | last post: by
4 posts views Thread by Slonocode | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.