473,763 Members | 7,044 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.InvalidO perationExcepti on: There was an error reflecting type
'Bos.BizObj.Eve nt'. ---> System.NotSuppo rtedException: The type
System.Collecti ons.Hashtable is not supported because it implements
IDictionary.
at System.Xml.Seri alization.TypeS cope.GetCollect ionElementType( 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 8716
using System;

using System.Collecti ons;

using System.Runtime. Serialization.F ormatters.Binar y;

using System.IO;
namespace ConsoleApplicat ion5

{

[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.A dd(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",Fi leMode.OpenOrCr eate
,FileAccess.Wri te );

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",Fi leMode.Open
,FileAccess.Rea d );

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 (DictionaryEntr y entry in NameToAddress )

{

nm3 = (name)entry.Key ;

ad3 = (address)entry. Value;

Console.WriteLi ne ("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******@betti nghouses.com_NO SPAM> wrote in message
news:OE******** ******@TK2MSFTN GP12.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.InvalidO perationExcepti on: There was an error reflecting type
'Bos.BizObj.Eve nt'. ---> System.NotSuppo rtedException: The type
System.Collecti ons.Hashtable is not supported because it implements
IDictionary.
at System.Xml.Seri alization.TypeS cope.GetCollect ionElementType( 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
3985
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 some objects, from a class I created myself. I read that I need to make my own "add" method, which I hopefully did correct. I hope someone can spot my wrongdoings, and point me in the right direction. The code is in the bottom of this post:
5
2368
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 here. But what? What do I want?
3
3040
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 back in the program with the "open" call. Hope someone can do something for me. Thanks,
5
2826
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 like it to return an XML serialized version of an object.
1
2338
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 reply "oh but what if the types in hastable don't have copy ctors defined? that could lead to dangerous situations". well, but was it a significant effort to provide a copy ctor / deep clone method to this class for objects that provide the...
2
1673
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 CaseInsensitiveComparer()); And deserializing it in 1.1? It doens't work, and a serialization exception is thrown indication that
3
1287
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 serialization of an object (i.e. by implementing the ISerializable interface). This object has a hashtable as one of its members, and this hashtable is
7
1795
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 which you can see I'm using when calling AddValue. I implemented ISerializable and GeteObjectData. Here's what I tried: for (int i = 0; i < this.Count; i++) info.AddValue("item" + i.ToString(), this, typeof(type) );
2
1337
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. when I try to serialize it with XmlSerializer, I get only the "class boundary" but my hashtable member doesn't get serialized. Hashtable is Serializable, at least that's what the documentation says.
0
9563
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
9386
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
10144
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9937
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,...
1
7366
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
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.