By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,256 Members | 1,548 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,256 IT Pros & Developers. It's quick & easy.

XML serialization of an Hashtable 2

P: n/a
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.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
Share this Question
Share on Google+
5 Replies


P: n/a
Posting again. Try not to repost the same message more then once.

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

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


--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"francois" <fr******@bettinghouses.com_NOSPAM> wrote in message
news:O9*************@TK2MSFTNGP11.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.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

P: n/a
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**********@tcon-NOSPAM.co.il> wrote in message
news:eO**************@TK2MSFTNGP10.phx.gbl...
Posting again. Try not to repost the same message more then once.

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

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


--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"francois" <fr******@bettinghouses.com_NOSPAM> wrote in message
news:O9*************@TK2MSFTNGP11.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.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 #3

P: n/a
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******@bettinghouses.com_NOSPAM> wrote in message
news:OQ*************@TK2MSFTNGP12.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**********@tcon-NOSPAM.co.il> wrote in message
news:eO**************@TK2MSFTNGP10.phx.gbl...
Posting again. Try not to repost the same message more then once.

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

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


--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"francois" <fr******@bettinghouses.com_NOSPAM> wrote in message
news:O9*************@TK2MSFTNGP11.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.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 #4

P: n/a
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.Length; 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 = ConnectionManager.GetBookieConnection();

try

{

connection.Open();

SelectedGamePersistent selectedGame = new SelectedGamePersistent(gameId,
connection);

selectedGame.IsLive = isLiveGame;

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

}

catch (Exception ex)

{

oEvent = null;

}

finally

{

connection.Close();

}

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**********@tcon-NOSPAM.co.il> wrote in message
news:OP**************@TK2MSFTNGP09.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******@bettinghouses.com_NOSPAM> wrote in message
news:OQ*************@TK2MSFTNGP12.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**********@tcon-NOSPAM.co.il> wrote in message
news:eO**************@TK2MSFTNGP10.phx.gbl...
Posting again. Try not to repost the same message more then once.

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

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

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"francois" <fr******@bettinghouses.com_NOSPAM> wrote in message
news:O9*************@TK2MSFTNGP11.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.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 #5

P: n/a
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******@bettinghouses.com_NOSPAM> wrote in message
news:uC**************@TK2MSFTNGP11.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.Length; 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 = ConnectionManager.GetBookieConnection();

try

{

connection.Open();

SelectedGamePersistent selectedGame = new SelectedGamePersistent(gameId,
connection);

selectedGame.IsLive = isLiveGame;

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

}

catch (Exception ex)

{

oEvent = null;

}

finally

{

connection.Close();

}

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**********@tcon-NOSPAM.co.il> wrote in message
news:OP**************@TK2MSFTNGP09.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******@bettinghouses.com_NOSPAM> wrote in message
news:OQ*************@TK2MSFTNGP12.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**********@tcon-NOSPAM.co.il> wrote in message
news:eO**************@TK2MSFTNGP10.phx.gbl...
> Posting again. Try not to repost the same message more then once.
>
> Regards,
> ---------------------------------------
>
> 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
> >
> >
>
>
>
> --
> Tamir Khason
> You want dot.NET? Just ask:
> "Please, www.dotnet.us "
>
>
> "francois" <fr******@bettinghouses.com_NOSPAM> wrote in message
> news:O9*************@TK2MSFTNGP11.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.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 #6

This discussion thread is closed

Replies have been disabled for this discussion.