C# 如何在c中序列化哈希表#
我已经实现了会话状态模式sqlserver,当我运行我的应用程序时,我面临哈希表的XML序列化错误。我的班级看起来像:C# 如何在c中序列化哈希表#,c#,serialization,hashtable,C#,Serialization,Hashtable,我已经实现了会话状态模式sqlserver,当我运行我的应用程序时,我面临哈希表的XML序列化错误。我的班级看起来像: [Serializable] public class ProjectSetup{ private System.Collections.Hashtable _ConfigTable; //and other properties here public System.Collections.Hashtable ConfigTable
[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”一种方法是在类上实现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);
}
}
使用ICollection接口实现使用自定义序列化,并将哈希表标记为[NonSerialized],相反,使用自定义集合而不是哈希表,或者在内部使用它来收集元素,如本例所示:
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;
}
}
试试这个:
这里详细介绍了序列化词典:
不管怎样,将字典序列化到JSON似乎是一个更自然的选择,所以考虑使用JSON序列化库对任何类都不适用,这不是来自BizEntity的,因此,通常,当我实现IXmlSerializable时,这是行不通的,然后它只为hastable属性创建xml,但我希望将整个类序列化。@Anil:将非序列化集合替换为您自己的序列化集合,如下图所示,你能给我一个完整的代码来链接序列化哈希表吗?序列化时用ArrayList替换哈希表。无论如何,如果我知道会话状态,它包含字符串类型的PAR:key,value,对吗?顺便说一句,如果你创建了一个注册帐户(完全免费等)你不会一直把所有的问题都丢在空位上