有关于Dictionary序列化,网上有许多。不过我都是没有成功应用;所以共享成功应用案例代码。
1.Dictionary本身是不支持序列化的,所以必须继承IXmlSerializable.
2.使用StringWriter进行最终String的转换工作,使用MemoryStream进行String输出,还是有异常报错。如:“十六进制Error!”
案例代码如下:
Step 1: Create Class ,Name Call SerializableDictionary
public class SerializableDictionary<TKey , TValue>
: Dictionary<TKey , TValue>
, IXmlSerializable {
#region IXmlSerializable 成员
public
XmlSchema GetSchema ( ) {
return null
;
}
/// <summary>
///
反序列化
/// </summary>
/// <param name="reader"></param>
public void
ReadXml ( XmlReader reader ) {
XmlSerializer keySerializer = new XmlSerializer( typeof
( TKey ) );
XmlSerializer valueSerializer = new XmlSerializer( typeof
( TValue ) );
if ( reader.IsEmptyElement || !
reader.Read() ) {
return
;
}
while ( reader.NodeType !=
XmlNodeType.EndElement ) {
reader.ReadStartElement( "item"
);
reader.ReadStartElement( "key"
);
TKey key =
( TKey )keySerializer.Deserialize( reader );
reader.ReadEndElement();
reader.ReadStartElement( "value"
);
TValue value =
( TValue )valueSerializer.Deserialize( reader );
reader.ReadEndElement();
reader.ReadEndElement();
reader.MoveToContent();
this
.Add( key , value );
}
reader.ReadEndElement();
}
/// <summary>
///
序列化
/// </summary>
/// <param name="writer"></param>
public void
WriteXml ( XmlWriter writer ) {
XmlSerializer keySerializer = new XmlSerializer( typeof
( TKey ) );
XmlSerializer valueSerializer = new XmlSerializer( typeof
( TValue ) );
foreach ( TKey key in this
.Keys ) {
writer.WriteStartElement( "item"
);
writer.WriteStartElement( "key"
);
keySerializer.Serialize( writer , key );
writer.WriteEndElement();
writer.WriteStartElement( "value"
);
valueSerializer.Serialize( writer , this
[key] );
writer.WriteEndElement();
writer.WriteEndElement();
}
}
#endregion
}
Step 2: Create Entity,Call Enity
[Serializable]
public class
Entity {
private
Int32 id;
public
Int32 ID {
get { return
id; }
set { id =
value; }
}
}
Step 3:Ready Demo Data
public static SerializableDictionary<Int32 , List<Entity>> GetDict ( ) {
SerializableDictionary<Int32 , List<Entity>> dict =
new SerializableDictionary<int , List<Entity>>();
{
List<Entity> el = new List<Entity>();
{
Entity entity = new Entity();
{
entity.ID = 1;
}
el.Add( entity );
}
dict.Add( 1 , el );
}
return dict;
}
Step 4:Coding Serializer and Deserialize For Dictionary
public static
String DictionarySerializer ( Object obj , System.Type type ) {
using ( StringWriter sw = new
StringWriter() ) {
XmlSerializer ser = new
XmlSerializer( type );
ser.Serialize( sw , obj );
return
sw.ToString();
}
}
public static
Object DictionaryDeserialize ( Type type , String str ) {
using ( StringReader sr = new
StringReader( str ) ) {
XmlSerializer xz = new
XmlSerializer( type );
return
xz.Deserialize( sr );
}
}
Step 5:WebService and AnyWhere ,Apply to this Case
Service Serialize:(服务方序列化)
[WebMethod]
public
String GetX ( Parameter query ) {
return
DictionarySerializer( Object ,
typeof( SerializableDictionary<Int32 , List<Entity>>
) );
}
Client Deserialize:(客户方反序列化)
SerializableDictionary<Int32 , List<Entity>> sd =
DictionaryDeserialize( typeof( SerializableDictionary<Int32 , List<Entity>>
)
,GetX( query ) )
as SerializableDictionary<Int32 , List<Entity>>;
Step 6: Output Results
<?xml version="1.0" encoding="utf-16"?>
<SerializableDictionaryOfInt32ListOfEntity>
<item>
<key>
<int>1</int>
</key>
<value>
<ArrayOfEntity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Entity>
<ID>1</ID>
</Entity>
</ArrayOfEntity>
</value>
</item>
</SerializableDictionaryOfInt32ListOfEntity>
参考资源:
http://www.mattberther.com/2004/06/14/serializing-an-idictionary-object/
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!114.entry