2009-08-19 16 views
11

संभव डुप्लिकेट:
Serialize Class containing Dictionary memberserializing नेट शब्दकोश

मैं एक शब्दकोश को क्रमानुसार कर सकते हैं?

+2

कौन सा क्रमबद्धता एपीआई:

यहाँ लिंक (कॉपीराइट (c) Dacris Software Inc. MIT लाइसेंस) से कोड है? वे प्रत्येक अलग होंगे ... –

उत्तर

19

कौन सी धारावाहिक API?

उदाहरण के लिए, DataContractSerializer शब्दकोशों को संभाल सकता है, खासकर (वैकल्पिक) [CollectionDataContract] मार्कअप के साथ। protobuf- नेट उन्हें (नीचे) संभाल लेंगे। अन्य लोग नहीं हो सकता ...

var data = new Dictionary<string, int>(); 
    data.Add("abc", 123); 
    data.Add("def", 456); 

    var clone = Serializer.DeepClone(data); 
    Console.WriteLine(clone["abc"]); 
    Console.WriteLine(clone["def"]); 

रूप BinaryFormatter करेंगे:

using (MemoryStream ms = new MemoryStream()) 
    { 
     var bf = new BinaryFormatter(); 
     bf.Serialize(ms, data); 
     ms.Position = 0; 
     clone = (Dictionary<string, int>) bf.Deserialize(ms); 
    } 
+0

+1। मैं शब्दकोश के लिए एक संभावित धारावाहिक के रूप में DataContractSerializer का सुझाव देने जा रहा था। यह बिना कहने के चला जाता है कि आप फोटोबफ-नेट का भी उल्लेख करेंगे। :-)। – RichardOD

+0

JSON.Net निश्चित रूप से शब्दकोशों को क्रमबद्ध कर सकता है। –

12

नहीं एक और क्रमबद्धता लाइब्रेरी का उपयोग कर या अपने आप को क्रमबद्धता को लागू करने के बिना। न तो हैशटेबल या डिक्शनरी .NET में बॉक्स से बाहर क्रमबद्ध है। एक SerializableDictionary here का कार्यान्वयन है जो एक्सएमएल और बाइनरी क्रमबद्धता दोनों करता है।

using System; 
using System.Runtime.Serialization; 
using System.Xml; 
using System.Xml.Serialization; 
using System.Collections.Generic; 
using System.Text; 

[Serializable()] 
public class SerializableDictionary<TKey, TVal> : Dictionary<TKey, TVal>, IXmlSerializable, ISerializable 
{ 
     #region Constants 
     private const string DictionaryNodeName = "Dictionary"; 
     private const string ItemNodeName = "Item"; 
     private const string KeyNodeName = "Key"; 
     private const string ValueNodeName = "Value"; 
     #endregion 
     #region Constructors 
     public SerializableDictionary() 
     { 
     } 

     public SerializableDictionary(IDictionary<TKey, TVal> dictionary) 
      : base(dictionary) 
     { 
     } 

     public SerializableDictionary(IEqualityComparer<TKey> comparer) 
      : base(comparer) 
     { 
     } 

     public SerializableDictionary(int capacity) 
      : base(capacity) 
     { 
     } 

     public SerializableDictionary(IDictionary<TKey, TVal> dictionary, IEqualityComparer<TKey> comparer) 
      : base(dictionary, comparer) 
     { 
     } 

     public SerializableDictionary(int capacity, IEqualityComparer<TKey> comparer) 
      : base(capacity, comparer) 
     { 
     } 

     #endregion 
     #region ISerializable Members 

     protected SerializableDictionary(SerializationInfo info, StreamingContext context) 
     { 
      int itemCount = info.GetInt32("ItemCount"); 
      for (int i = 0; i < itemCount; i++) 
      { 
       KeyValuePair<TKey, TVal> kvp = (KeyValuePair<TKey, TVal>)info.GetValue(String.Format("Item{0}", i), typeof(KeyValuePair<TKey, TVal>)); 
       this.Add(kvp.Key, kvp.Value); 
      } 
     } 

     void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) 
     { 
      info.AddValue("ItemCount", this.Count); 
      int itemIdx = 0; 
      foreach (KeyValuePair<TKey, TVal> kvp in this) 
      { 
       info.AddValue(String.Format("Item{0}", itemIdx), kvp, typeof(KeyValuePair<TKey, TVal>)); 
       itemIdx++; 
      } 
     } 

     #endregion 
     #region IXmlSerializable Members 

     void IXmlSerializable.WriteXml(System.Xml.XmlWriter writer) 
     { 
      //writer.WriteStartElement(DictionaryNodeName); 
      foreach (KeyValuePair<TKey, TVal> kvp in this) 
      { 
       writer.WriteStartElement(ItemNodeName); 
       writer.WriteStartElement(KeyNodeName); 
       KeySerializer.Serialize(writer, kvp.Key); 
       writer.WriteEndElement(); 
       writer.WriteStartElement(ValueNodeName); 
       ValueSerializer.Serialize(writer, kvp.Value); 
       writer.WriteEndElement(); 
       writer.WriteEndElement(); 
      } 
      //writer.WriteEndElement(); 
     } 

     void IXmlSerializable.ReadXml(System.Xml.XmlReader reader) 
     { 
      if (reader.IsEmptyElement) 
      { 
       return; 
      } 

      // Move past container 
      if (!reader.Read()) 
      { 
       throw new XmlException("Error in Deserialization of Dictionary"); 
      } 

      //reader.ReadStartElement(DictionaryNodeName); 
      while (reader.NodeType != XmlNodeType.EndElement) 
      { 
       reader.ReadStartElement(ItemNodeName); 
       reader.ReadStartElement(KeyNodeName); 
       TKey key = (TKey)KeySerializer.Deserialize(reader); 
       reader.ReadEndElement(); 
       reader.ReadStartElement(ValueNodeName); 
       TVal value = (TVal)ValueSerializer.Deserialize(reader); 
       reader.ReadEndElement(); 
       reader.ReadEndElement(); 
       this.Add(key, value); 
       reader.MoveToContent(); 
      } 
      //reader.ReadEndElement(); 

      reader.ReadEndElement(); // Read End Element to close Read of containing node 
     } 

     System.Xml.Schema.XmlSchema IXmlSerializable.GetSchema() 
     { 
      return null; 
     } 

     #endregion 
     #region Private Properties 
     protected XmlSerializer ValueSerializer 
     { 
      get 
      { 
       if (valueSerializer == null) 
       { 
        valueSerializer = new XmlSerializer(typeof(TVal)); 
       } 
       return valueSerializer; 
      } 
     } 

     private XmlSerializer KeySerializer 
     { 
      get 
      { 
       if (keySerializer == null) 
       { 
        keySerializer = new XmlSerializer(typeof(TKey)); 
       } 
       return keySerializer; 
      } 
     } 
     #endregion 
     #region Private Members 
     private XmlSerializer keySerializer = null; 
     private XmlSerializer valueSerializer = null; 
     #endregion 
} 
+0

क्या आप इस तरह के लिंक से कोड कॉपी करने के लिए बहुत दयालु होंगे? –

+0

हो गया, क्लिक करने के लिए एक कम लिंक। –

+0

@ScottLerch यह अभी भी सच है? – Thomas

संबंधित मुद्दे