how to serialize hashtable in c#

爷,独闯天下 提交于 2019-12-23 09:36:43

问题


I have implemented session state mode sqlserver and when i run my application i am facing XML serialization error of hash table . and my class looks like:

[Serializable]
    public class ProjectSetup{
    private System.Collections.Hashtable _ConfigTable;
   //and other properties here

   public System.Collections.Hashtable ConfigTable
        {
            get { return _ConfigTable; }
        }

}

Now i want to know how to serialize the hastable or if there is another alternative please let me know.

and exactly error is: "Cannot serialize member ProjectSetup.ConfigTable of type System.Collections.Hashtable, because it implements IDictionary"


回答1:


Try this:

http://www.deploymentzone.com/2008/09/19/idictionarytkeytvalue-ixmlserializable-and-lambdas/




回答2:


One way is to implement IXmlSerializable on your class and serialize the hashtable manually. See this article for more detail.

public void WriteXml(System.Xml.XmlWriter writer)
{
    // Used while Serialization

    // Serialize each BizEntity this collection holds
    foreach( string key in this.Dictionary.Keys )
    {
        Serializer.Serialize(writer, this.Dictionary[key]);
    }
}

public void ReadXml(System.Xml.XmlReader reader)
{
    // Used while Deserialization

    // Move past container
    reader.Read();

    // Deserialize and add the BizEntitiy objects
    while( reader.NodeType != XmlNodeType.EndElement )
    {
        BizEntity entity;

        entity = Serializer.Deserialize(reader) as BizEntity;
        reader.MoveToContent();
        this.Dictionary.Add(entity.Key, entity);
    }
}



回答3:


Serializing dictionaries is well-described here: http://weblogs.asp.net/avnerk/archive/2006/05/23/Serilalizing-Dictionaries.aspx .

Anyhow serializing dictionaries to JSON seems a more natural choice so consider using JSON serialization libraries




回答4:


Use custom serialization using ICollection interface implementation, and mark Hashtable as [NonSerialized], instead, use custom collection instead of Hashtable or use it internally, for collection of elements, as in this example:

  using System;
  using System.IO;
  using System.Collections;
  using System.Xml.Serialization;

  public class Test{
      static void Main(){
          Test t = new Test();
          t.SerializeCollection("coll.xml");
      }

      private void SerializeCollection(string filename){
          Employees Emps = new Employees();
          // Note that only the collection is serialized -- not the 

          // CollectionName or any other public property of the class.

          Emps.CollectionName = "Employees";
          Employee John100 = new Employee("John", "100xxx");
          Emps.Add(John100);
          XmlSerializer x = new XmlSerializer(typeof(Employees));
          TextWriter writer = new StreamWriter(filename);
          x.Serialize(writer, Emps);
      }
  }
  public class Employees:ICollection{
      public string CollectionName;
      private ArrayList empArray = new ArrayList(); 

      public Employee this[int index]{
          get{return (Employee) empArray[index];}
      }

      public void CopyTo(Array a, int index){
          empArray.CopyTo(a, index);
      }
      public int Count{
          get{return empArray.Count;}
      }
      public object SyncRoot{
          get{return this;}
      }
      public bool IsSynchronized{
          get{return false;}
      }
      public IEnumerator GetEnumerator(){
          return empArray.GetEnumerator();
      }

      public void Add(Employee newEmployee){
          empArray.Add(newEmployee);
      }
  }

  public class Employee{
      public string EmpName;
      public string EmpID;
      public Employee(){}
      public Employee(string empName, string empID){
          EmpName = empName;
          EmpID = empID;
      }
  }


来源:https://stackoverflow.com/questions/7413828/how-to-serialize-hashtable-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!