473,603 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Serializing into an XML file a Hashtable

I need to serialize into an XML file a hashtable.
From MSDN at XmlSerializer:
http://msdn.microsoft.com/library/de...classtopic.asp

I implemented the indexable property and the add function but I'm still
getting the following exception:"The type System.Collecti ons.Hashtable
is not supported because it implements IDictionary."
My code for Item and Add add-on looks like this:
Expand|Select|Wrap|Line Numbers
  1. [Serializable]
  2. public class DaysArchive
  3. {
  4.  
  5. public class HashTableItems
  6. {
  7. private IDictionaryEnumerator HashEntries;
  8. public HashTableItems()
  9. {
  10. }
  11.  
  12. public  void Initialize(Hashtable table)
  13. {
  14. HashEntries = table.GetEnumerator();
  15. }
  16.  
  17. public System.Collections.DictionaryEntry this[string key]
  18. {
  19. get
  20. {
  21. HashEntries.Reset();
  22. HashEntries.MoveNext();
  23. while ((string)HashEntries.Key != key)
  24. {
  25. HashEntries.MoveNext();
  26. }
  27. return new DictionaryEntry(HashEntries.Key,HashEntries.Value);
  28. }
  29. }
  30. }
  31.  
  32. public readonly HashTableItems Item;
  33. public Hashtable archive;
  34.  
  35. public DaysArchive()
  36. {
  37. archive = new Hashtable();
  38. this.Item = new HashTableItems();
  39. this.Item.Initialize(archive);
  40. }
  41.  
  42. public void AddItem(string Name, bool offline)
  43. {
  44. if (archive.ContainsKey(Name) == false)
  45. archive.Add(Name,offline);
  46. }
  47.  
  48. public bool GetKeyValue(string Name)
  49. {
  50. if (archive.ContainsKey(Name) == false)
  51. throw new Exception("Key could not be found!");
  52.  
  53. IDictionaryEnumerator days_enum = archive.GetEnumerator();
  54.  
  55.  
  56. days_enum.MoveNext();
  57. while (((string)days_enum.Key != Name)&&(days_enum.MoveNext()));
  58.  
  59. return (bool)days_enum.Value;
  60. }
  61.  
  62. public bool IsKeyInCollection(string Name)
  63. {
  64. return archive.ContainsKey(Name);
  65. }
  66.  
  67. public void Add(System.Collections.DictionaryEntry cel)
  68. {
  69. archive.Add(cel.Key,cel.Value);
  70. }
  71. }
  72.  
Nov 18 '05 #1
5 2514
Hi Victor,

I am probably missing something, what do you use HashTableItems for ?
What is the role of this part of the code :

HashEntries.Res et();
HashEntries.Mov eNext();
while ((string)HashEn tries.Key != key)
{
HashEntries.Mov eNext();
}

Are you trying to check the existence of a key ? Such type of
functionality is implemented by a Hashtable.

What is the role of DaysArchive ? Do you want to store (Name,
offline) pairs ? A Hashtable should be sufficient for this type of
functionality.
Please feel free to contact me if you need any more information.

Regards,
Bart

http://www.xenopz.com/blog/bartdeboeck/

Nov 19 '05 #2
Hi Bart,

HashTableItems is a custom made class which I use to define the suport
for the Item indexed property. I make an instance of this class and I
return it in the get method of the Item property.

My goal is to serialize the Hashtable into an XML file. I you try to
serialize a hashtable the XmlSerializer throws an exception that says
Hashtable impelements IDictionary. Now I wrote my own class which has
an internal Hashtable. In the hash I store a string wich is the name of
a file and a boolean variable which represents the state of the file
data (if it was obtain from a the realtime-market or tha data was
aquired when the market was offline)
I modiffied the implementation of the whole DaysArchive class as follows
but I still get an exception that says "You must implement the default
accessor for DaysArchive because it inherits ICollection"
I still have not a clue what am I supposed to do next.

Thank you very much for your support.
Expand|Select|Wrap|Line Numbers
  1. public class DaysArchive:ICollection,IEnumerable
  2. {
  3. private Hashtable archive;
  4. private HashItems hashitems;
  5.  
  6. public DaysArchive()
  7. {
  8. archive  = new Hashtable();
  9. hashitems = new HashItems();
  10. hashitems.Refresh( archive);
  11. }
  12.  
  13. #region implementations for ICollection & IEnumerable
  14.  
  15. public int Count
  16. {
  17. get
  18. {
  19. return archive.Count;
  20. }
  21. }
  22.  
  23.  
  24. public bool IsSynchronized
  25. {
  26. get
  27. {
  28. return archive.IsSynchronized;
  29. }
  30. }
  31.  
  32.  
  33. public object SyncRoot
  34. {
  35. get
  36. {
  37. return archive.SyncRoot;
  38. }
  39. }
  40.  
  41.  
  42. public void CopyTo(    Array array,int index)
  43. {
  44. archive.CopyTo(array,index);
  45. }
  46.  
  47.  
  48. public IEnumerator GetEnumerator()
  49. {
  50. return archive.GetEnumerator();
  51. }
  52.  
  53.  
  54. #endregion
  55.  
  56. public bool ContainsKey(string Name)
  57. {
  58. return archive.ContainsKey(Name);
  59. }
  60.  
  61.  
  62. public void Add(DictionaryEntry cel)
  63. {
  64. archive.Add(cel.Key,cel.Value);
  65. hashitems.Refresh(archive);
  66. }
  67.  
  68.  
  69. public void Add(object key, object value)
  70. {
  71. archive.Add(key,value);
  72. hashitems.Refresh( archive);
  73. }
  74.  
  75.  
  76. public class HashItems
  77. {
  78. Hashtable hash;
  79.  
  80. public HashItems(Hashtable hash)
  81. {
  82. this.hash = hash;
  83. }
  84. public HashItems()
  85. {
  86. }
  87.  
  88. public DictionaryEntry this[int index]
  89. {
  90. get
  91. {
  92. IDictionaryEnumerator enumerator = hash.GetEnumerator();
  93.  
  94. enumerator.MoveNext();
  95.  
  96. while(index>0)
  97. {
  98. enumerator.MoveNext();
  99. index--;
  100. }
  101. return new DictionaryEntry(enumerator.Key,enumerator.Value);
  102. }
  103. }
  104.  
  105. public void Refresh( Hashtable hash)
  106. {
  107. this.hash = hash;
  108. }
  109.  
  110. public int Count
  111. {
  112. get
  113. {
  114. return hash.Count;
  115. }
  116. }
  117. }
  118.  
  119.  
  120. public HashItems Item
  121. {
  122. get
  123. {
  124. return hashitems;
  125. }
  126. }
  127. }
  128.  
Nov 19 '05 #3
Hi Victor,

Thanks for the extra info. I'll take a look at your question with the
extra info tomorrow.

cu,
Bart

http://www.xenopz.com/blog/bartdeboeck/

Victor Paraschiv schreef:
Hi Bart,

HashTableItems is a custom made class which I use to define the suport
for the Item indexed property. I make an instance of this class and I
return it in the get method of the Item property.

My goal is to serialize the Hashtable into an XML file. I you try to
serialize a hashtable the XmlSerializer throws an exception that says
Hashtable impelements IDictionary. Now I wrote my own class which has
an internal Hashtable. In the hash I store a string wich is the name of
a file and a boolean variable which represents the state of the file
data (if it was obtain from a the realtime-market or tha data was
aquired when the market was offline)
I modiffied the implementation of the whole DaysArchive class as follows
but I still get an exception that says "You must implement the default
accessor for DaysArchive because it inherits ICollection"
I still have not a clue what am I supposed to do next.

Thank you very much for your support.
Expand|Select|Wrap|Line Numbers
  1.      public class DaysArchive:ICollection,IEnumerable
  2.      {
  3.          private Hashtable archive;
  4.          private HashItems hashitems;
  5.          public DaysArchive()
  6.          {
  7.              archive  = new Hashtable();
  8.              hashitems = new HashItems();
  9.              hashitems.Refresh( archive);
  10.          }
  11.          #region implementations for ICollection & IEnumerable
  12.          public int Count
  13.          {
  14.              get
  15.              {
  16.                  return archive.Count;
  17.              }
  18.          }
  19.          public bool IsSynchronized
  20.          {
  21.              get
  22.              {
  23.                  return archive.IsSynchronized;
  24.              }
  25.          }
  26.          public object SyncRoot
  27.          {
  28.              get
  29.              {
  30.                  return archive.SyncRoot;
  31.              }
  32.          }
  33.          public void CopyTo(    Array array,int index)
  34.          {
  35.              archive.CopyTo(array,index);
  36.          }
  37.          public IEnumerator GetEnumerator()
  38.          {
  39.              return archive.GetEnumerator();
  40.          }
  41.          #endregion
  42.          public bool ContainsKey(string Name)
  43.          {
  44.              return archive.ContainsKey(Name);
  45.          }
  46.          public void Add(DictionaryEntry cel)
  47.          {
  48.              archive.Add(cel.Key,cel.Value);
  49.              hashitems.Refresh(archive);
  50.          }
  51.          public void Add(object key, object value)
  52.          {
  53.              archive.Add(key,value);
  54.              hashitems.Refresh( archive);
  55.          }
  56.          public class HashItems
  57.          {
  58.              Hashtable hash;
  59.              public HashItems(Hashtable hash)
  60.              {
  61.                  this.hash = hash;
  62.              }
  63.              public HashItems()
  64.              {
  65.              }
  66.              public DictionaryEntry this[int index]
  67.              {
  68.                  get
  69.                  {
  70.                      IDictionaryEnumerator enumerator = hash.GetEnumerator();
  71.                      enumerator.MoveNext();
  72.                      while(index>0)
  73.                      {
  74.                          enumerator.MoveNext();
  75.                          index--;
  76.                      }
  77.                      return new DictionaryEntry(enumerator.Key,enumerator.Value);
  78.                  }
  79.              }
  80.              public void Refresh( Hashtable hash)
  81.              {
  82.                  this.hash = hash;
  83.              }
  84.              public int Count
  85.              {
  86.                  get
  87.                  {
  88.                      return hash.Count;
  89.                  }
  90.              }
  91.          }
  92.          public HashItems Item
  93.          {
  94.              get
  95.              {
  96.                  return hashitems;
  97.              }
  98.          }
  99.      }
  100.  


Nov 21 '05 #4
Hi Victor,

This link http://www.thecodeproject.com/csharp/hash_ser.asp is not an
answer, it might, however, provide a few hints.

Regards,
Bart

http://www.xenopz.com/blog/bartdeboeck/

Nov 21 '05 #5
Problem solved. I discarded everything was written in MSDN library at
XmlSerializer.
I've built a custom class that contains internally a Hashtable and
inherits ICollection and IEnumerable. I added then an indexer which
returns the hashtableitem at the given index. That's all!

Victor Paraschiv

ba**********@ho tmail.com wrote:
Hi Victor,

This link http://www.thecodeproject.com/csharp/hash_ser.asp is not an
answer, it might, however, provide a few hints.

Regards,
Bart

http://www.xenopz.com/blog/bartdeboeck/

Nov 23 '05 #6

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

Similar topics

3
3319
by: Earl Teigrob | last post by:
I am considering writing a Class that Selects, Adds, Updates and Deletes Nodes in an XML File but do not what to reinvent the wheel. (See XML file below) That data format would emulate records in a Database Table with a Primary Key for each Record (see xml sample below) and a flat file structure. I would use a class to manipulate this type of data structure extensivly if I had one. Does anyone know of such an animal?
3
3164
by: Don McNamara | last post by:
Hi, I've hit quite a strange problem with XmlSerializer on my W2K3 server. When I serialize/deserialize using an exe on my local computer (XP), everything works fine. When I put the code out on the server (W2K3) it throws an exception. It only seems to happen when serializing/deserializing _arrays_ of a type. If I just serialize/deserialize one instance, it works fine. The exception I get is: (sorry for the word wrapping.)...
0
1399
by: Kenneth Baltrinic | last post by:
Is the following code correct for serializing a quasi-single class, that is a class that has a descreet (though more than one, so not a true singleton) number of static instances and no dynamically creatable instances? Please forgive my use of terms. Quasi-signleton is a term I made up and static instances is a little bogus too but I don't know the correct terms for what I mean if there are any. However, I think the code below should be...
11
3597
by: hoopsho | last post by:
Hi Everyone, I am trying to write a program that does a few things very fast and with efficient use of memory... a) I need to parse a space-delimited file that is really large, upwards fo a million lines. b) I need to store the contents into a unique hash. c) I need to then sort the data on a specific field. d) I need to pull out certain fields and report them to the user.
3
10151
by: MioTheGreat | last post by:
I know how to take a single hashtable, and then use a binaryformatter and a filestream to dump it to a file, but I need to serialize and deserialize a hashtable inside a class. I've been trying this: public Hashtable Directions; public byte DirectionsSerialized { get
13
5237
by: Leszek Taratuta | last post by:
Hello, I have several drop-down lists on my ASP.NET page. I need to keep data sources of these lists in Session State. What would be the most effective method to serialize this kind of data structures? Thanks for any hints, Leszek Taratuta
2
1315
by: Doug Eller | last post by:
I am serializing datasets and sending them out to a client application via a web service. My problem is that the associated schema includes all of my connection string, database username and password and SQL info. This is bad. I can think of two ways to deal with this: 1. Parse the data out of the schema before i send it out 2. Encrypt the data My question is......is there a better way?
0
1617
by: JackRazz | last post by:
I'm trying to serialize a collection to a file stream by serializing each object individually. The code below works fine with the BinaryFormatter, but the SoapFormatter reads the first object and just goes to the end of the file. After reading the first object, the fStream.Position is pointed to the end of the file. The collection serializes/deserilizes fine when I just serialize the hashtable vs. each object in the hashtable. Does...
0
1105
by: Michael Maercker | last post by:
Hi! I'm about to go nuts over my serializing problem. This is my situation: I have a Data-Class that can have children of the same class which are stored in a hashtable, i.e: X has A as a child A has B as a child When an object is changed it raises an Updated-Event which the
0
7996
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
8415
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
8060
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6735
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
5878
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
5441
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();...
0
3951
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2430
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
1
1514
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.