473,396 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.Collections.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 2481
Hi Victor,

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

HashEntries.Reset();
HashEntries.MoveNext();
while ((string)HashEntries.Key != key)
{
HashEntries.MoveNext();
}

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**********@hotmail.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
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...
3
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...
0
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...
11
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...
3
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...
13
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...
2
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...
0
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...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.