c#中如何序列化哈希表

本文关键字:序列化 哈希表 | 更新日期: 2023-09-27 18:08:27

我已经实现了会话状态模式sqlserver,当我运行我的应用程序时,我面临哈希表的XML序列化错误。我的类是这样的:

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

现在我想知道如何序列化hastable,或者如果有其他替代方案请告诉我。

,确切的错误是:"不能序列化成员ProjectSetup。ConfigTable的类型是System.Collections。哈希表,因为它实现了字典"

c#中如何序列化哈希表

试试这个:

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

一种方法是在类上实现IXmlSerializable并手动序列化哈希表。详情请参阅本文。

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);
    }
}

序列化字典这里有很好的描述:http://weblogs.asp.net/avnerk/archive/2006/05/23/Serilalizing-Dictionaries.aspx。

无论如何,将字典序列化为JSON似乎是一个更自然的选择,所以考虑使用JSON序列化库

使用ICollection接口实现使用自定义序列化,并将Hashtable标记为[NonSerialized],相反,使用自定义集合代替Hashtable或在内部使用它,用于元素集合,如以下示例:

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