【问题标题】:Key Value Storage in Settings file设置文件中的键值存储
【发布时间】:2009-07-22 16:25:39
【问题描述】:

我正在用 C# 开发一个应用程序,它需要在设置文件中存储一个键值对。 我试图在设置文件中保存字典的数组列表,但它失败了。这是我所做的:

if (Settings1.Default.arraylst == null)
       {
           Settings1.Default.arraylst = new System.Collections.ArrayList();
       }

       Dictionary<string, string> dd = new Dictionary<string, string>();
       dd.Add("1", "1");

       Settings1.Default.arraylst.Add(dd);              
       Settings1.Default.Save();

当我重新启动应用程序时,arrarylist 变为空..

提前谢谢....

【问题讨论】:

    标签: c#


    【解决方案1】:

    这是因为通用字典由于某些原因无法序列化,请尝试使用这个

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml.Serialization;
    
    [XmlRoot("dictionary")]
    public class SerializableDictionary<TKey, TValue>
        : Dictionary<TKey, TValue>, IXmlSerializable
    {
        #region IXmlSerializable Members
        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        } 
        public void ReadXml(System.Xml.XmlReader reader)
        {
            XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
            XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
            bool wasEmpty = reader.IsEmptyElement;
            reader.Read();
    
            if (wasEmpty)
                return;
            while (reader.NodeType != System.Xml.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();
                this.Add(key, value);
                reader.ReadEndElement();
                reader.MoveToContent();
            }
            reader.ReadEndElement();
        }
        public void WriteXml(System.Xml.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");
                TValue value = this[key];
                valueSerializer.Serialize(writer, value);
                writer.WriteEndElement();
                writer.WriteEndElement();
            }
        }
        #endregion
    }
    

    我在这里找到XML Serializable Generic Dictionary

    【讨论】:

      【解决方案2】:

      C#设置文件只是XML文件,只会保存可序列化的类,否则保存时不会写出数据。不幸的是,字典不可序列化。

      一种解决方案是构建您自己的包装器来正确序列化字典。 This link 可能会让您了解其中涉及的内容。

      另一种解决方案是将您的键/值对写成一个长字符串数组,并在您读回它时对其进行解析。

      //Define a string[] in your settings
      
      List<string> keyValues = new List<string>();
      foreach(KeyValuePair<string,string> pair in dd)
      {
        keyValues.Add(pair.Key);
        keyValues.Add(pair.Value);
      }
      Settings1.Default.KeyValuePairs = keyValues.ToArray();
      

      然后在以类似方式加载时回读这些对。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-22
        • 1970-01-01
        • 1970-01-01
        • 2012-06-23
        • 1970-01-01
        • 2022-01-01
        相关资源
        最近更新 更多