2011-09-14 30 views
6

Tôi đã triển khai sqlserver chế độ trạng thái phiên và khi tôi chạy ứng dụng của mình, tôi đang đối mặt với lỗi tuần tự hóa XML của bảng băm. và lớp học của tôi trông giống như:cách serialize hashtable trong C#

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

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

} 

Bây giờ tôi muốn biết làm thế nào để sắp xếp hàng hóa nhanh hoặc nếu có cách thay thế khác, vui lòng cho tôi biết.

và chính xác lỗi là: "Không thể serialize thành viên ProjectSetup.ConfigTable loại System.Collections.Hashtable, bởi vì nó thực hiện IDictionary"

+0

Thay Hashtable với ArrayList trong khi serializing –

+0

Dù sao, nếu tôi có ý tưởng của bạn về trạng thái phiên, nó chứa pars kiểu string: chìa khóa, giá trị, tôi có đúng không? –

+1

btw, nếu bạn tạo một tài khoản đã đăng ký (hoàn toàn miễn phí vv), bạn sẽ không mất tất cả câu hỏi của mình vào khoảng trống –

Trả lời

0

Một cách là để thực hiện IXmlSerializable trên lớp học của bạn và serialize Hashtable bằng tay. Xem this article để biết thêm chi tiết.

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

Điều đó sẽ không hoạt động đối với bất kỳ lớp nào, không có nguồn gốc từ BizEntity. 't làm việc –

+0

ok khi tôi thực hiện IXmlSerializable sau đó nó tạo xml chỉ cho thuộc tính hastable nhưng tôi muốn toàn bộ lớp được serialized. – Anil

+0

@Anil: Thay thế các bộ sưu tập không được sắp xếp theo thứ tự của riêng bạn, như được hiển thị bên dưới –

0

Sử dụng tùy chỉnh serialization sử dụng thực hiện giao diện ICollection, và đánh dấu Hashtable như [NonSerialized], thay vào đó, sử dụng bộ sưu tập tùy chỉnh thay vì Hashtable hoặc sử dụng nó trong nội bộ, cho bộ sưu tập của các yếu tố, như trong ví dụ này:

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