473,734 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XML serialization of an Hashtable 2

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.

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
5 2823
Posting again. Try not to repost the same message more then once.

Regards,
---------------------------------------

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


--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"francois" <fr******@betti nghouses.com_NO SPAM> wrote in message
news:O9******** *****@TK2MSFTNG P11.phx.gbl... 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.

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
The problem is that I think your code is not related to my question, I found
the same code doing a google search -
http://radio.weblogs.com/0111551/sto...erialized.html
- and it is not helping me in my situation, then before posting stuff, make
sure you answer the question, if you cannot answer, just let a chance for
someone to do it.

Tx
"Tamir Khason" <ta**********@t con-NOSPAM.co.il> wrote in message
news:eO******** ******@TK2MSFTN GP10.phx.gbl...
Posting again. Try not to repost the same message more then once.

Regards,
---------------------------------------

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


--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"francois" <fr******@betti nghouses.com_NO SPAM> wrote in message
news:O9******** *****@TK2MSFTNG P11.phx.gbl...
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.

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 #3
The question was "How can I make the Hashtable serializable ", right? This
code snaplet is very famous in i-net, however if the answer can not halp
you, I'm sorry. Please provide exact situation and what you want to do?
If I'm using this code on your structure, it works fine without any
exception. So what is the problem?
Please help me and others to clarify and help you to solve it.
--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"francois" <fr******@betti nghouses.com_NO SPAM> wrote in message
news:OQ******** *****@TK2MSFTNG P12.phx.gbl...
The problem is that I think your code is not related to my question, I found the same code doing a google search -
http://radio.weblogs.com/0111551/sto...erialized.html - and it is not helping me in my situation, then before posting stuff, make sure you answer the question, if you cannot answer, just let a chance for
someone to do it.

Tx
"Tamir Khason" <ta**********@t con-NOSPAM.co.il> wrote in message
news:eO******** ******@TK2MSFTN GP10.phx.gbl...
Posting again. Try not to repost the same message more then once.

Regards,
---------------------------------------

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


--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"francois" <fr******@betti nghouses.com_NO SPAM> wrote in message
news:O9******** *****@TK2MSFTNG P11.phx.gbl...
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.

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 #4
First of all thanks a lot and here i will go for more details but i am
afraid it may be a little bit lenghty.

This is the full definition of my class:

[Serializable]
public class Event
{
private int eventId;
private EventTypeId eventTypeId;
private EventActionId eventActionId;
private string parameter;
//private Hashtable hParameters;

public int EventId
{
get
{
return eventId;
}
}

public EventTypeId EventTypeID //EventTypeId is an enum of int type
{
get
{
return eventTypeId;
}
}

public EventActionId EventActionID //EventActionId is an enum of int
type
{
get
{
return eventActionId;
}
}

public Hashtable Parameters
{
get
{
Hashtable hParameters = new Hashtable();
string[] paramCouples = parameter.Split ('&');
for (int i = 0; i < paramCouples.Le ngth; i++)
{
string[] keyValue = paramCouples[i].Split('=');
hParameters.Add (keyValue[0], keyValue[1]);
}
return hParameters;
}
}
public Event()
{
}
then inside a webservice I have the following code :

[WebMethod]

public Event SetGameLive(int gameId, bool isLiveGame)

{

Event oEvent;

SqlConnection connection = ConnectionManag er.GetBookieCon nection();

try

{

connection.Open ();

SelectedGamePer sistent selectedGame = new SelectedGamePer sistent(gameId,
connection);

selectedGame.Is Live = isLiveGame;

selectedGame.Up dateSelectedGam e(connection, out oEvent); // here my
oEvent object is instanciated an returned as an out parameter.

}

catch (Exception ex)

{

oEvent = null;

}

finally

{

connection.Clos e();

}

return oEvent;

}
When I run the webservice, the Event object is being XML serialized
automatically by the run time and then I did not implement any specific
method in the Event object to manually XML serialize my object.
From what I see in your code, you are serializing the hashtable in a binary
fashion to save it in a file isn't it? But I need an XML serialization.

Then should I serialize my hashtable manually? How? And how can i replace
the default serialization mechanism by my own one if I need to serialize the
object manually?

And on top of all is why Hashtable is not serializable as it implements
ISerializable interface?

best regards,

Francois
"Tamir Khason" <ta**********@t con-NOSPAM.co.il> wrote in message
news:OP******** ******@TK2MSFTN GP09.phx.gbl...
The question was "How can I make the Hashtable serializable ", right? This
code snaplet is very famous in i-net, however if the answer can not halp
you, I'm sorry. Please provide exact situation and what you want to do?
If I'm using this code on your structure, it works fine without any
exception. So what is the problem?
Please help me and others to clarify and help you to solve it.
--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"francois" <fr******@betti nghouses.com_NO SPAM> wrote in message
news:OQ******** *****@TK2MSFTNG P12.phx.gbl...
The problem is that I think your code is not related to my question, I found
the same code doing a google search -

http://radio.weblogs.com/0111551/sto...erialized.html
- and it is not helping me in my situation, then before posting stuff,

make
sure you answer the question, if you cannot answer, just let a chance for
someone to do it.

Tx
"Tamir Khason" <ta**********@t con-NOSPAM.co.il> wrote in message
news:eO******** ******@TK2MSFTN GP10.phx.gbl...
Posting again. Try not to repost the same message more then once.

Regards,
---------------------------------------

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
>
>

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"francois" <fr******@betti nghouses.com_NO SPAM> wrote in message
news:O9******** *****@TK2MSFTNG P11.phx.gbl...
> 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.
>
> 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 #5
OK, Not it's clear, but I have gloomy answer for you.
Q: Why can't I serialize hashtables?

A: The XmlSerializer cannot process classes implementing the IDictionary
interface. This was partly due to schedule constraints and partly due to the
fact that a hashtable does not have a counterpart in the XSD type system.
The only solution is to implement a custom hashtable that does not implement
the IDictionary interface.

The other option (not tested) is to use SoapFormater for binary
serialization.

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:uC******** ******@TK2MSFTN GP11.phx.gbl...
First of all thanks a lot and here i will go for more details but i am
afraid it may be a little bit lenghty.

This is the full definition of my class:

[Serializable]
public class Event
{
private int eventId;
private EventTypeId eventTypeId;
private EventActionId eventActionId;
private string parameter;
//private Hashtable hParameters;

public int EventId
{
get
{
return eventId;
}
}

public EventTypeId EventTypeID //EventTypeId is an enum of int type
{
get
{
return eventTypeId;
}
}

public EventActionId EventActionID //EventActionId is an enum of int type
{
get
{
return eventActionId;
}
}

public Hashtable Parameters
{
get
{
Hashtable hParameters = new Hashtable();
string[] paramCouples = parameter.Split ('&');
for (int i = 0; i < paramCouples.Le ngth; i++)
{
string[] keyValue = paramCouples[i].Split('=');
hParameters.Add (keyValue[0], keyValue[1]);
}
return hParameters;
}
}
public Event()
{
}
then inside a webservice I have the following code :

[WebMethod]

public Event SetGameLive(int gameId, bool isLiveGame)

{

Event oEvent;

SqlConnection connection = ConnectionManag er.GetBookieCon nection();

try

{

connection.Open ();

SelectedGamePer sistent selectedGame = new SelectedGamePer sistent(gameId,
connection);

selectedGame.Is Live = isLiveGame;

selectedGame.Up dateSelectedGam e(connection, out oEvent); // here my
oEvent object is instanciated an returned as an out parameter.

}

catch (Exception ex)

{

oEvent = null;

}

finally

{

connection.Clos e();

}

return oEvent;

}
When I run the webservice, the Event object is being XML serialized
automatically by the run time and then I did not implement any specific
method in the Event object to manually XML serialize my object.
From what I see in your code, you are serializing the hashtable in a binary fashion to save it in a file isn't it? But I need an XML serialization.

Then should I serialize my hashtable manually? How? And how can i replace
the default serialization mechanism by my own one if I need to serialize the object manually?

And on top of all is why Hashtable is not serializable as it implements
ISerializable interface?

best regards,

Francois
"Tamir Khason" <ta**********@t con-NOSPAM.co.il> wrote in message
news:OP******** ******@TK2MSFTN GP09.phx.gbl...
The question was "How can I make the Hashtable serializable ", right? This
code snaplet is very famous in i-net, however if the answer can not halp
you, I'm sorry. Please provide exact situation and what you want to do?
If I'm using this code on your structure, it works fine without any
exception. So what is the problem?
Please help me and others to clarify and help you to solve it.
--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"francois" <fr******@betti nghouses.com_NO SPAM> wrote in message
news:OQ******** *****@TK2MSFTNG P12.phx.gbl...
The problem is that I think your code is not related to my question, I found
the same code doing a google search -

http://radio.weblogs.com/0111551/sto...erialized.html
- and it is not helping me in my situation, then before posting stuff,
make
sure you answer the question, if you cannot answer, just let a chance for someone to do it.

Tx
"Tamir Khason" <ta**********@t con-NOSPAM.co.il> wrote in message
news:eO******** ******@TK2MSFTN GP10.phx.gbl...
> Posting again. Try not to repost the same message more then once.
>
> Regards,
> ---------------------------------------
>
> 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
> >
> >
>
>
>
> --
> Tamir Khason
> You want dot.NET? Just ask:
> "Please, www.dotnet.us "
>
>
> "francois" <fr******@betti nghouses.com_NO SPAM> wrote in message
> news:O9******** *****@TK2MSFTNG P11.phx.gbl...
> > 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.
> >
> > 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 #6

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

Similar topics

3
3984
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
2366
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
3038
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,
1
8713
by: francois | last post by:
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: public class Event {
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
1670
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
1285
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
1793
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
1335
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
8946
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
8776
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
9310
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...
0
9182
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
8186
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
6735
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
6031
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();...
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
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.