我想把程序中的一个大的hashtable 写入到配置文件当中  像这样:        public static Dictionary<string, CakeInfo> CityList = new Dictionary<string, CakeInfo>
        {
            { "草莓", new CakeInfo { CakeAcname = "caomei", MaxSize = 50, MaxThread = 1, Interval = 5 } },
            { "西瓜", new CakeInfo { CakeAcname = "xigua", MaxSize = 50, MaxThread = 1, Interval = 5 } },     
            { "香蕉", new CakeInfo { CakeAcname = "xiangjiao", MaxSize = 50, MaxThread = 1, Interval = 5 } },
            { "苹果", new CakeInfo { CakeAcname = "pingguo", MaxSize = 50, MaxThread = 1, Interval = 5 } },
      ...
        };

解决方案 »

  1.   

    像你那样:
            public static Hashtable ht = new Hashtable
            {
                {"a", "s"},
                {"b", "s"}
            };
      

  2.   

    程序代码中我这样写了        public static Dictionary<string, CakeInfo> CityList = new Dictionary<string, CakeInfo> { System.Configuration.ConfigurationSettings.AppSettings["mystring"] };配置文件当中我这样写了 <add key ="mystring" value="
     { ""草莓"", new CakeInfo { CakeAcname = ""caomei"", MaxSize = 50, MaxThread = 1, Interval = 5 } },
                { ""西瓜"", new CakeInfo { CakeAcname = ""xigua"", MaxSize = 50, MaxThread = 1, Interval = 5 } },     
                { ""香蕉"", new CakeInfo { CakeAcname = ""xiangjiao"", MaxSize = 50, MaxThread = 1, Interval = 5 } },
                { ""苹果"", new CakeInfo { CakeAcname = ""pingguo"", MaxSize = 50, MaxThread = 1, Interval = 5 } }
               "/>但这样是不行的,第一个“草莓”的前面的引号处会提示“缺少属性值”。 程序中会提示方法没有一个参数的重载,就是程序把读取的字符串当做一个参数了。
      

  3.   

    之前也遇到过,奇怪的是字典不支持XML序列化,后来是通过另外编写了一个类实现的,用的时候可以先转化成字典
    下面是我保存和读取配置文件使用的基类,希望对你有用
    public class ConfigurationSectionHandlerBase<T>:IConfigurationSectionHandler where T : ConfigurationSectionHandlerBase<T>
        {
            protected static string configName = "ApplicationConfig";
            protected static T _instance;        /// <summary>
            /// 配置文件实例
            /// </summary>
            [XmlIgnore()]
            public static T Instance
            {
                get
                {
                    if (_instance == null)
                    {
                        lock (typeof(T))
                        {
                            if (_instance == null)
                            {
                                _instance = ConfigurationManager.GetSection(configName) as T;                            if (_instance == null)
                                {
                                    _instance = CreateInstance();
                                    _instance.SaveConfig();
                                }
                            }
                        }
                    }                return _instance;
                }
            }        /// <summary>
            /// 配置文件所使用的编码
            /// </summary>
            [XmlIgnore()]
            public Encoding Encoding
            {
                set
                {
                    _encoding = value;
                }
                get
                {
                    return _encoding;
                }
            }   private Encoding _encoding;        /// <summary>
            /// 保存配置
            /// </summary>
            public void SaveConfig()
            {
                if (_instance == null)
                {
                    return;
                }            SerializerFactory<T> factory = new SerializerFactory<T>();
                ISerializer<T> serializer = factory.GetSerializer();            string xml = serializer.Serialize(_instance);            string configFile = Application.ExecutablePath + ".config";            XmlDocument configDoc = new XmlDocument();            configDoc.Load(configFile);
                XmlNode node = configDoc.DocumentElement.SelectSingleNode(configName);
                if (node != null)
                {
                    configDoc.DocumentElement.RemoveChild(node);
                }            //xml = xml.Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>", "");
                //xml = "<Data>" + xml + "</Data>";            XmlDocument nodeDoc = new XmlDocument();
                nodeDoc.LoadXml(xml);            node = nodeDoc.SelectSingleNode(configName);
                node = configDoc.ImportNode(node, true);            configDoc.DocumentElement.AppendChild(node);            configDoc.Save(configFile);        }        private static T CreateInstance()
            {
                T instance = ReflectionHelper.GetObject(typeof(T)) as T;
                return instance;
            }        #region IConfigurationSectionHandler Members        public object Create(object parent, object configContext, System.Xml.XmlNode section)
            {
                string outXml = section.OuterXml;
                XmlSerializer<T> serializer = new XmlSerializer<T>();
                T o = serializer.Deserialize(outXml);            return o;
            }        #endregion
        }
      

  4.   

    Dictionary支持XML,直接序列化出去就可以了.
    也可以先序列成一段XML String,再把String存到配置中去.
      

  5.   

    关键我要的是程序从hashtable 中读取信息,而hashtable中的信息不能在程序中出现,只能保存在app.config中。而配置文件中又不能使用尖括号(<>)。我看网上有这种方法:hashtable mynamevalue= (hashtable)system.configuration.configurationsettings.appsettings.get(@"myconfig\mydictionary");
    string area = mynamevalue["area"];
    string device= mynamevalue["device"];
    string customer = mynamevalue["customer "];但是不会用。拜托各位再给想想办法。其实xml的序列化我只会简单的,第一次遇到hashtable 这种。
      

  6.   

    我还是没想到解决方案 所以我把hashtable 拆开了 不过还是谢谢大家的帮助