【问题标题】:how to serialize hashtable in c#如何在c#中序列化哈希表
【发布时间】:2011-11-16 20:22:11
【问题描述】:

我已经实现了会话状态模式 sqlserver,当我运行我的应用程序时,我面临哈希表的 XML 序列化错误。我的班级看起来像:

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

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

}

现在我想知道如何序列化 hastable 或者如果有其他选择请告诉我。

确切的错误是:“无法序列化 System.Collections.Hashtable 类型的成员 ProjectSetup.ConfigTable,因为它实现了 IDictionary”

【问题讨论】:

  • 在序列化时用 ArrayList 替换 Hashtable
  • 无论如何,如果我明白你对会话状态的看法,它包含字符串类型的 pars:键、值,对吗?
  • 顺便说一句,如果您创建了一个注册帐户(完全免费等),您将不会一直丢失所有问题

标签: c# serialization hashtable


【解决方案1】:
【解决方案2】:

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

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

【讨论】:

  • 这不适用于任何不是从 BizEntity 派生的类,因此,一般来说,这不起作用
  • 好的,当我实现 IXmlSerializable 然后它只为 hastable 属性创建 xml 但我希望整个类被序列化。
  • @Anil:把非序列化的集合替换成你自己序列化的集合,如下图
  • 你能给我一个完整的链接代码hurricanesoftwares.com/serialize-hash-table-in-c-dynamically 用于序列化哈希表
【解决方案3】:

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

无论如何,将字典序列化为 JSON 似乎是更自然的选择,因此请考虑使用 JSON 序列化库

【讨论】:

    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 2013-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-07
      • 1970-01-01
      • 2016-02-07
      • 2015-03-12
      • 1970-01-01
      相关资源
      最近更新 更多