473,407 Members | 2,320 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,407 software developers and data experts.

XML serialization of an Hashtable

Hi,

I have a webservice that I am using and I would like it to return an XML
serialized version of an object.

the class of the object is defined serializable as the following:

[Serializable]
public class Event
{
}

Also it contains 5 properties returning simple data type such as int and
string. But one of them is an Hashtable. When I run my project i got a run
time exception telling me that :

System.InvalidOperationException: There was an error reflecting type
'Bos.BizObj.Event'. ---> System.NotSupportedException: The type
System.Collections.Hashtable is not supported because it implements
IDictionary.
at System.Xml.Serialization.TypeScope.GetCollectionEl ementType(Type type)

Then my question is:
How can I make the Hashtable serializable to make in turn my Event class
serializable?

Also I am a little bit surprised as in the .Net framework reference it is
specified that the
Hashtable class implement the serializable interface... Then it is puzzling
me even more

Thanks a lot,

Francois
Nov 16 '05 #1
1 8681
using System;

using System.Collections;

using System.Runtime.Serialization.Formatters.Binary;

using System.IO;
namespace ConsoleApplication5

{

[Serializable]

struct name

{

public string forname;

public string familyname;

}

[Serializable]

struct address

{

public string street;

public int number;

}

class clsDataBlock

{

public static Hashtable NameToAddress = new Hashtable ();

[STAThread]

static void Main(string[] args)

{

clsDataBlock db=new clsDataBlock ();

string ans="";

while("x" != ans)

{

System.Console.WriteLine ("1 to enter new");

System.Console.WriteLine ("2 to search");

System.Console.WriteLine ("3 to save (serialize)");

System.Console.WriteLine ("4 to load (deserialize)");

System.Console.WriteLine ("5 to show data");

System.Console.WriteLine ("x to quit");

System.Console.WriteLine ("-----------------------");

ans = System.Console.ReadLine ();

switch(ans)

{

case "1":db.AddNew();

break;

case "2":db.Search();

break;

case "3":db.Save();

break;

case "4":db.Load();

break;

case "5":db.Show();

break;

}

}

}

public void AddNew()

{

name nm;

address ad;

string ans;

System.Console.WriteLine ("enter first name (x to exit):");

ans = System.Console.ReadLine();

nm.forname = ans;

System.Console.WriteLine ("enter last name (x to exit):");

ans = System.Console.ReadLine();

nm.familyname = ans;

System.Console.WriteLine ("enter street name (x to exit):");

ans = System.Console.ReadLine();

ad.street = ans;

System.Console.WriteLine ("enter street number (x to exit):");

ans = System.Console.ReadLine();

ad.number = int.Parse (ans);

NameToAddress.Add(nm,ad);

}

public void Search()

{

name nm2;

string ans2;

System.Console.WriteLine ("enter first name:");

ans2 = System.Console.ReadLine ();

nm2.forname = ans2;

System.Console.WriteLine ("enter last name:");

ans2 = System.Console.ReadLine ();

nm2.familyname = ans2;

System.Console.WriteLine ("working...");

object sname = NameToAddress[nm2];

if (sname != null)

{

address ad2 = (address)sname;

System.Console.WriteLine(" address: "+ ad2.street + " " +
ad2.number );

}

else

System.Console.WriteLine ("no name like that...");

}

public void Save()

{

FileStream fs = new FileStream ("Store.dat",FileMode.OpenOrCreate
,FileAccess.Write );

try

{

Hashtable a = new Hashtable();

a = NameToAddress;

BinaryFormatter bf=new BinaryFormatter ();

bf.Serialize (fs,a );

}

finally

{

fs.Close ();

}

}

public void Load()

{

FileStream fs = new FileStream ("Store.dat",FileMode.Open
,FileAccess.Read );

try

{

Hashtable a = new Hashtable();

BinaryFormatter bf=new BinaryFormatter ();

a=(Hashtable)bf.Deserialize (fs);

NameToAddress = a;

}

finally

{

fs.Close ();

}

}

public void Show()

{

name nm3;

address ad3;

foreach (DictionaryEntry entry in NameToAddress )

{

nm3 = (name)entry.Key;

ad3 = (address)entry.Value;

Console.WriteLine ("name= {0} {1}, address= {2} {3}",

nm3.forname,nm3.familyname ,ad3.street ,ad3.number);

}

}

}

}
Hope it helps
--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"francois" <fr******@bettinghouses.com_NOSPAM> wrote in message
news:OE**************@TK2MSFTNGP12.phx.gbl...
Hi,

I have a webservice that I am using and I would like it to return an XML
serialized version of an object.

the class of the object is defined serializable as the following:

[Serializable]
public class Event
{
}

Also it contains 5 properties returning simple data type such as int and
string. But one of them is an Hashtable. When I run my project i got a run
time exception telling me that :

System.InvalidOperationException: There was an error reflecting type
'Bos.BizObj.Event'. ---> System.NotSupportedException: The type
System.Collections.Hashtable is not supported because it implements
IDictionary.
at System.Xml.Serialization.TypeScope.GetCollectionEl ementType(Type type)

Then my question is:
How can I make the Hashtable serializable to make in turn my Event class
serializable?

Also I am a little bit surprised as in the .Net framework reference it is
specified that the
Hashtable class implement the serializable interface... Then it is puzzling me even more

Thanks a lot,

Francois

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Cederstrom | last post by:
Hello group! :) I want to create a config/status object, that will contain a Hashtable. This object I would like to save through Serialization, but im having some trouble. My hashtable contain...
5
by: Arjen | last post by:
Hello, Can somebody help me a little bit? I can't get it to work. Please see my code below... I have placed some comments like "// And whats next?". I'm sure that I have to code something...
3
by: Arjen | last post by:
Hi there, I have tried to run some samples without succes. So I have made a new sample. Maybe someone can fix the serialization? The class "MyProgram" must be saved to an xml file and placed...
5
by: francois | last post by:
First of all I would to to apologize for resending this post again but I feel like my last post as been spoiled Here I go for my problem: Hi, I have a webservice that I am using and I would...
1
by: oDDskOOL | last post by:
I realized today that the Hashtable.Clone only produces a shallow copy... that makes me go mad that M$ doesn't even provide a deep copy ctor for the Hashtable class ! mighty tech ducks might...
2
by: Brian Keating | last post by:
hi there, has anyone had success in serializing a 2.0 case sensivite hashtable ie. private Hashtable m_itemsById = new Hashtable( new CaseInsensitiveHashCodeProvider(), new...
3
by: Wild Wind | last post by:
Hello, I've been battling over this problem for the better part of a day, so I'd appreciate it if someone could shed some light here. I have a file which is produced by the custom binary...
7
by: Joe | last post by:
I've tracked the performance issue down to a single class. This class derives from CollectionBase and stores a basic value type such as string, int, double, etc... I also store the type itself...
2
by: Ron M. Newman | last post by:
Hi, I have a simple class that has a Hashtable. the hashtable has a couple of key/value pairs where the key is a string and the value is also a strong. I have at the top of that class. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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,...

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.