?
Solved

Serialization of System.Collections.Specialized.StringDictionary

Posted on 2005-03-02
2
Medium Priority
?
1,444 Views
Last Modified: 2012-05-05
I need a custom subclass of the StringDictionary, which supports serialization.

and I need a helper class to serialize to a System.String, and not to a file.
 
0
Comment
Question by:bvrudney
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 5

Accepted Solution

by:
fahimnxb earned 1500 total points
ID: 13447606
0
 
LVL 2

Author Comment

by:bvrudney
ID: 13450125
I have tried several different things, and can't get anything to work.  I've tried inheriting from StringDictionary, and finally, I've reflected the StringDictionary code, and just ported it right into my class.  I keep getting obscure errors from the xmlserializer:

"You must implement the Add(System.Object) method on PropertyDictionary because it inherits from IEnumerable."

If I remove the IEnumerable interface, I get this error:

You must implement a default accessor on System.Collections.ICollection because it inherits from ICollection.

which doesn't make any since, since it has an indexer.

Here is the code I'm using to serialize, along with my custom PropertyDictionary class:

private string SerializeCollection(PropertyDictionary pd)
{
      StringBuilder sb = new StringBuilder();
      XmlSerializer serializer = new XmlSerializer( typeof( PropertyDictionary ));
      TextWriter sw = new StringWriter( sb );

      serializer.Serialize( sw, pd );

      sw.Close();
      return sb.ToString();
}


using System;
using System.Runtime.Serialization;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel.Design.Serialization;
using System.Globalization;
using System.Reflection;

[Serializable]
public class PropertyDictionary: IEnumerable, ISerializable,IDeserializationCallback
{
      SerializationInfo _savedSI;
      private Hashtable contents;

      public PropertyDictionary()
      {
            this.contents = new Hashtable();
      }

      public PropertyDictionary(SerializationInfo info, StreamingContext ctxt)
      {
            _savedSI = info;
      }
      void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
      {

            String[] keys  = new String[contents.Count];
            String[] values = new String[contents.Count];

            info.AddValue("Keys",   keys);
            info.AddValue("Values", values);
      }
      void IDeserializationCallback.OnDeserialization(Object sender)
      {

            String[] keys   = (String[])_savedSI.GetValue("Keys", typeof(String[]));
            String[] values = (String[])_savedSI.GetValue("Values", typeof(String[]));
            for (Int32 x = 0; x < keys.Length; x++)
            {
                  this.Add(keys[x], values[x]);
            }
            _savedSI = null;
      }
      public virtual void Add(string key, string value)
      {
            this.contents.Add(key.ToLower(CultureInfo.InvariantCulture), value);
      }
      public virtual void Clear()
      {
            this.contents.Clear();
      }
      public virtual bool ContainsKey(string key)
      {
            return this.contents.ContainsKey(key.ToLower(CultureInfo.InvariantCulture));
      }
      public virtual bool ContainsValue(string value)
      {
            return this.contents.ContainsValue(value);
      }
      public virtual void CopyTo(Array array, int index)
      {
            this.contents.CopyTo(array, index);
      }
      public virtual IEnumerator GetEnumerator()
      {
            return this.contents.GetEnumerator();
      }
      public virtual void Remove(string key)
      {
            this.contents.Remove(key.ToLower(CultureInfo.InvariantCulture));
      }
      public virtual int Count
      {
            get
            {
                  return this.contents.Count;
            }
      }
      public virtual bool IsSynchronized
      {
            get
            {
                  return this.contents.IsSynchronized;
            }
      }
      public virtual string this[string key]
      {
            get
            {
                  return (string) this.contents[key.ToLower(CultureInfo.InvariantCulture)];
            }
            set
            {
                  this.contents[key.ToLower(CultureInfo.InvariantCulture)] = value;
            }
      }
      public virtual ICollection Keys
      {
            get
            {
                  return this.contents.Keys;
            }
      }
      public virtual object SyncRoot
      {
            get
            {
                  return this.contents.SyncRoot;
            }
      }
      public virtual ICollection Values
      {
            get
            {
                  return this.contents.Values;
            }
      }
}
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question