如何在C#中序列化哈希表

6

我已经实现了 sqlserver 的会话状态模式,但是当我运行程序时,遇到了哈希表的 XML 序列化错误。我的类看起来像:

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

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

}

现在我想知道如何序列化哈希表,或者如果有其他替代方法,请告诉我。
具体错误是:"无法序列化类型为System.Collections.Hashtable的ProjectSetup.ConfigTable成员,因为它实现了IDictionary"。

将Hashtable替换为ArrayList进行序列化。 - Alan Turing
无论如何,如果我理解你的会话状态的想法,它包含了字符串类型的部分:键、值,我是对的吗? - Alan Turing
1
顺便说一下,如果您创建一个注册账户(完全免费等),您就不会再失去所有的问题了。 - Marc Gravell
4个回答


0
一种方法是在您的类上实现IXmlSerializable,并手动序列化hashtable。有关更多详细信息,请参见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的类,因此通常情况下不起作用。 - Alan Turing
当我实现IXmlSerializable时,它只为哈希表属性创建XML,但我希望整个类都能被序列化。 - Anil
@Anil:将非序列化集合替换为您自己的序列化集合,如下所示。 - Alan Turing
你能给我提供一个完整的代码,用于序列化哈希表吗?链接为http://www.hurricanesoftwares.com/serialize-hash-table-in-c-dynamically/。 - Anil

0

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

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接