473,804 Members | 2,280 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

System.Collecti ons.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 5412
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.Seri alization;
using System.Collecti ons;

namespace Utils
{
public class XmlSerializable Hashtable : IXmlSerializabl e, IEnumerable
{
private Hashtable _table;

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

private XmlSerializable Hashtable(Hasht able table)
{
this._table = table;
}

void IXmlSerializabl e.ReadXml(XmlRe ader reader)
{
reader.Read();
while (reader.NodeTyp e != XmlNodeType.End Element)
{
reader.ReadStar tElement("item" );
string key = reader.ReadElem entString("key" );
string value = reader.ReadElem entString("valu e");
reader.ReadEndE lement();
reader.MoveToCo ntent();
_table.Add(key, value);
}
}

void IXmlSerializabl e.WriteXml(XmlW riter writer)
{
foreach (object key in _table.Keys)
{
object value = _table[key];
writer.WriteSta rtElement("item ");
writer.WriteEle mentString("key ", key.ToString()) ;
writer.WriteEle mentString("val ue", value.ToString( ));
writer.WriteEnd Element();
}
}

System.Xml.Sche ma.XmlSchema IXmlSerializabl e.GetSchema()
{
return null;
}

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

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

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(obj ect key)
{
return _table.Contains Key(key);
}

public bool ContainsValue(o bject key)
{
return _table.Contains Value(key);
}

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

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

public IEnumerator GetEnumerator()
{
return new Enumerator(_tab le.GetEnumerato r());
}

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

public Enumerator(IEnu merator inner)
{
this.inner = inner;
}

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

public object Current
{
get { return ((DictionaryEnt ry) 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
17216
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 define the Add, Contains and Remove methods is an IList, and we all should know that a set isn't a list. ICollection only defines Count and CopyTo, which isn't useful at all. So even if I want to write my own Set implementation, what interface...
6
2256
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??? 2) And also, just curious, Hashtable.Add (key, value), can i somehow make the hashtable to have this Add(key, value1, value2).
1
5524
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 designtime. Thank you! Sky
3
8035
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 element with the same key value as an existing one is added. For instance, is the following code legal in C#? hashtable.Add("aaa", "foo"); hashtable.Add("aaa", "bar");
3
10170
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 this: public Hashtable Directions; public byte DirectionsSerialized { get
3
1677
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 original object? thanks
4
1079
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
1242
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
16770
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
9714
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9594
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10346
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10347
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10090
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9173
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7635
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.