Dictionary搴忓垪鍖栧拰鍙嶅簭鍒楀寲

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dictionary搴忓垪鍖栧拰鍙嶅簭鍒楀寲相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/type' title='type'>type   dict   his   iter   form   des   Matter   formatter   ret   

   //瀹氫箟鍙簭鍒楀寲Dictionary绫?
銆€銆€[Serializable]
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable { public SerializableDictionary() { } public void WriteXml(XmlWriter write) // Serializer { XmlSerializer KeySerializer = new XmlSerializer(typeof(TKey)); XmlSerializer ValueSerializer = new XmlSerializer(typeof(TValue)); foreach (KeyValuePair<TKey, TValue> kv in this) { write.WriteStartElement("SerializableDictionary"); write.WriteStartElement("key"); KeySerializer.Serialize(write, kv.Key); write.WriteEndElement(); write.WriteStartElement("value"); ValueSerializer.Serialize(write, kv.Value); write.WriteEndElement(); write.WriteEndElement(); } } public void ReadXml(XmlReader reader) // Deserializer { reader.Read(); XmlSerializer KeySerializer = new XmlSerializer(typeof(TKey)); XmlSerializer ValueSerializer = new XmlSerializer(typeof(TValue)); while (reader.NodeType != XmlNodeType.EndElement) { reader.ReadStartElement("SerializableDictionary"); reader.ReadStartElement("key"); TKey tk = (TKey)KeySerializer.Deserialize(reader); reader.ReadEndElement(); reader.ReadStartElement("value"); TValue vl = (TValue)ValueSerializer.Deserialize(reader); reader.ReadEndElement(); reader.ReadEndElement(); this.Add(tk, vl); reader.MoveToContent(); } reader.ReadEndElement(); } public XmlSchema GetSchema() { return null; } }

搴忓垪鍖栵細

using (FileStream fileStream = new FileStream(fileName, FileMode.Create))  
{  
    XmlSerializer xmlFormatter = new XmlSerializer(typeof(SerializableDictionary<string, string>));  
    xmlFormatter.Serialize(fileStream, this.serializableDictionary);  
}  

鍙嶅簭鍒楀寲锛?/p>

using (FileStream fileStream = new FileStream(fileName, FileMode.Open))  
{  
    XmlSerializer xmlFormatter = new XmlSerializer(typeof(SerializableDictionary<string, string>));  
    this.serializableDictionary = (SerializableDictionary<string,string>)xmlFormatter.Deserialize(fileStream);  
}  

 

 


以上是关于Dictionary搴忓垪鍖栧拰鍙嶅簭鍒楀寲的主要内容,如果未能解决你的问题,请参考以下文章

92 搴忓垪鍖? 鍙嶅簭鍒楀寲

浜屽弶鏍戠殑搴忓垪鍖栦笌鍙嶅簭鍒楀寲

鏁版嵁搴忓垪鍖栨牸寮?YAML

springboot椤圭洰涓娇鐢╯pring-data-Redis瀵筸ap搴忓垪鍖栨椂鎶ラ敊

Dubbo 楂樺嵄婕忔礊锛佸師鏉ラ兘鏄弽搴忓垪鍖栨児寰楃ジ

Jmeter鑴氭湰鍙傛暟鍖栧拰姝e垯鍖归厤