473,216 Members | 2,241 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,216 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 5384
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Mart | last post by:
What class does everyone out there use if they want to store a set of values efficiently? In java I use a HashSet, but there is no equivalent in C#. Even worse, the lowest level interface to...
6
by: Chua Wen Ching | last post by:
Hi there, 1) I am looking for the best collections techniques to be used in my program. Is hashtable or arraylist or any .net collection, which is the fastest when adding, getting out data???...
1
by: Sky Sigal | last post by:
Hello: Is there a way to get the path of the current website I am working on during designTime (when current Context == null)? System.Web.HttpRuntime.AppDomainAppPath is not available during...
3
by: nzanella | last post by:
Hello, I cannot seem to find this in the MSDN documentation: what I would like to ask is whether the System.Collections.Hashtable.Add should throw an exception or replace the old value when an...
3
by: MioTheGreat | last post by:
I know how to take a single hashtable, and then use a binaryformatter and a filestream to dump it to a file, but I need to serialize and deserialize a hashtable inside a class. I've been trying...
3
by: jw56578 | last post by:
I have an object that inherits from hashtable and is serializable to be stored in the viewstate, but when I retrieve it from viewstate, it is converted into a hashtable, why won't it stay as the...
4
by: Slonocode | last post by:
I have a hashtable full of objects. There seems to be no way to get an object from the hashtable and modify it. Are collections or hashtables in particular not meant for this kind of operation?
0
by: Simon Hart | last post by:
Hi, I have the following code which is miss-behaving at one site: public class HMRHost { private int _pid;
3
by: semedao | last post by:
Hi, what is the best way to perform deep copy (Clone) on class that inherit from System.Collections.Generic.Dictionary ?
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.