2011-09-14 51 views
6

He implementado el modo de estado de sesión sqlserver y cuando ejecuto mi aplicación estoy enfrentando el error de serialización XML de la tabla hash. y mi clase se parece a:cómo serializar hashtable en C#

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

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

} 

Ahora quiero saber cómo serializar el hastable o si hay otra alternativa por favor hágamelo saber.

y exactamente error es: "No se puede serializar miembro de ProjectSetup.ConfigTable de tipo System.Collections.Hashtable, ya que implementa IDictionary"

+0

Reemplazar Hashtable con ArrayList, mientras que la serialización –

+0

De todos modos, si me dieron su idea del estado de la sesión, que contiene pares de tipo cadena llave, valor, estoy en lo cierto? –

+1

Por cierto, si creas una cuenta registrada (completamente gratis, etc.) no seguirás perdiendo todas tus preguntas en el vacío –

Respuesta

0

Una forma es aplicar IXmlSerializable en su clase y serializar la tabla hash manualmente. Ver this article para más detalles.

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

Eso no funcionaría para ninguna clase, que no se deriva de BizEntity, por lo que, en común, eso no lo haría 't work –

+0

bien cuando implemente IXmlSerializable, entonces creo xml solo para el atributo hastable, pero quiero que se serialice toda la clase. – Anil

+0

@Anil: reemplace las colecciones no serializadas con sus propias series, como se muestra a continuación –

0

uso de serialización personalizado utilizando implementación de la interfaz ICollection, y la marca de Hashtable como [NonSerialized], en cambio, utilizar la colección personalizada en lugar de Hashtable o utilizarlo internamente, para la recolección de elementos, como en este ejemplo:

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