我想把程序中的一个大的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 } },
...
};
{
{ "草莓", 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 } },
...
};
解决方案 »
- 高手求解 递归和树目录问题
- DataGridView的问题
- 这样的窗体是怎么做出来的
- 想了解EnterpriseObjects.dll的出处,或者告诉我怎么去查?
- wpf3d漫游效果
- 向高手请教异步编程的问题先放100分,不够再开帖补分。
- 一个关于控制摄像头的问题!!!!!!
- 怎样从Active Dicretory导出信息到DB中?
- C#里面虚拟按键是怎么写的??是不是用那个SendKeys()?哪位给点代码,谢谢~~
- VS2012里面的Reporting是什么项目呢
- 关于WPF 加载和改变Image,引起抖动的问题
- serialPort1_DataReceived事件中处理串口的返回值,程序会死掉
public static Hashtable ht = new Hashtable
{
{"a", "s"},
{"b", "s"}
};
{ ""草莓"", 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 } }
"/>但这样是不行的,第一个“草莓”的前面的引号处会提示“缺少属性值”。 程序中会提示方法没有一个参数的重载,就是程序把读取的字符串当做一个参数了。
下面是我保存和读取配置文件使用的基类,希望对你有用
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
}
也可以先序列成一段XML String,再把String存到配置中去.
string area = mynamevalue["area"];
string device= mynamevalue["device"];
string customer = mynamevalue["customer "];但是不会用。拜托各位再给想想办法。其实xml的序列化我只会简单的,第一次遇到hashtable 这种。