473,382 Members | 1,375 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,382 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 5390
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 ?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.