自定义配置XML设置并存储到字典对象
问题描述:
我打算设置为FormFields配置键,查询字符串参数等。在我的web.config我已经设置如下:自定义配置XML设置并存储到字典对象
<WhiteListPaametersGroup>
<WhiteListPaameters>
<FormField1>EVENTVALIDATION</FormField1>
<FormField2>VIEWSTATE</FormField2>
<FormField3>Button1</FormField3>
<QueryString1>firstname</QueryString1>
<QueryString2>lastname</QueryString2>
</WhiteListPaameters>
</WhiteListPaametersGroup>
然后在我的代码,我读取值如下:
Dictionary<string, string> parameters = new Dictionary<string,string>();
foreach (XmlNode n in section.ChildNodes)
{
parameters.Add(n.Name, n.InnerText);
}
有没有更好的存储方式。稍后,我希望能够像字典一样查看字典,并且能够获得FormFields,Querystrings等的设置。
请让我知道我是否可以写得更清洁。
感谢
答
您可以使用XML序列来存储您的设置,直接恢复到它们的对象。这不是完美的,但很容易设置,并为您提供对象保存/恢复。
有这个类(属性必须是public):
public class WhiteListParameters
{
public string FormField1 { get; set; }
public string FormField2 { get; set; }
public string FormField3 { get; set; }
public string QueryString1 { get; set; }
public string QueryString2 { get; set; }
}
将它保存到XML文件,运行此代码:
WhiteListParameters parms = new WhiteListParameters
{
FormField1 = "EVENTVALIDATION",
FormField2 = "VIEWSTATE",
FormField3 = "Button1",
QueryString1 = "firstname",
QueryString2 = "lastname"
};
using(StreamWriter sw = new StreamWriter("C:\\temp\\config.xml"))
{
XmlSerializer xs = new XmlSerializer(typeof (WhiteListParameters));
xs.Serialize(sw, parms);
sw.Close();
}
要读它回到对象:
using(StreamReader sr = new StreamReader("c:\\temp\\config.xml"))
{
XmlSerializer xs = new XmlSerializer(typeof (WhiteListParameters));
WhiteListParameters parms = (WhiteListParameters) xs.Deserialize(sr);
sr.Close();
}
答
您可能会考虑的一个选项是创建自定义配置部分,您可以在web.config
。
下面是我对我们引用XSLT模板创建一个配置部分:
namespace Foo.Web.Applications.CustomConfigurationSections
{
public class XslTemplateConfiguration : ConfigurationSection
{
[ConfigurationProperty("xslTemplates")]
public XslTemplateElementCollection XslTemplates
{
get { return this["xslTemplates"] as XslTemplateElementCollection; }
}
}
public class XslTemplateElement : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true, IsKey = true)]
public string Name
{
get { return this["name"] as string; }
set { this["name"] = value; }
}
[ConfigurationProperty("path", IsRequired = true)]
public string Path
{
get { return this["path"] as string; }
set { this["path"] = value; }
}
}
public class XslTemplateElementCollection : ConfigurationElementCollection
{
public XslTemplateElement this[object key]
{
get { return base.BaseGet(key) as XslTemplateElement; }
}
public override ConfigurationElementCollectionType CollectionType
{
get { return ConfigurationElementCollectionType.BasicMap; }
}
protected override string ElementName
{
get { return "xslTemplate"; }
}
protected override bool IsElementName(string elementName)
{
bool isName = false;
if (!String.IsNullOrEmpty(elementName))
isName = elementName.Equals("xslTemplate");
return isName;
}
protected override ConfigurationElement CreateNewElement()
{
return new XslTemplateElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((XslTemplateElement)element).Name;
}
}
}
如下您可以注册在web.config本节:
<configSections>
<section name="xslTemplateConfiguration" type="Foo.Web.Applications.CustomConfigurationSections.XslTemplateConfiguration"/>
</configSections>
,您可以访问集合在你的代码是这样的:
var config = WebConfigurationManager.OpenWebConfiguration("/");
if (config.HasFile)
{
var templates = config.GetSection("xslTemplateConfiguration") as XslTemplateConfiguration;
if (templates != null)
{
var templatePath = templates.XslTemplates["PO"].Path;
}
}
答
对于阅读配置,.NET的Fra mework在System.Configuration命名空间中有很多类。最重要的课程是ConfigurationSection和ConfigurationElement。从这些类派生,添加你想要的属性并用ConfigurationProperty属性来修饰它们。这种方法的好处是类型安全,可以定义有效的事实和事实,.NET Framework框架将自动完成所有的读取,解析和检查web.config中的值。
如果你使用的是vs2010,看看这个扩展,其惊人的:http://visualstudiogallery.msdn.microsoft.com/def289f1-377d-4cd0-802a-80c8be8b6758 – asawyer 2012-04-20 22:17:54