C# 自定义配置xml设置并存储到dictionary对象
我计划为FormFields、QueryString参数等设置配置键。在我的web.config中,我有如下设置:C# 自定义配置xml设置并存储到dictionary对象,c#,asp.net,xml,web-config,C#,Asp.net,Xml,Web Config,我计划为FormFields、QueryString参数等设置配置键。在我的web.config中,我有如下设置: <WhiteListPaametersGroup> <WhiteListPaameters> <FormField1>EVENTVALIDATION</FormField1> <FormField2>VIEWSTATE</FormField2> <FormField3>B
<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);
}
<configSections>
<section name="xslTemplateConfiguration" type="Foo.Web.Applications.CustomConfigurationSections.XslTemplateConfiguration"/>
</configSections>
事件验证
视图状态
按钮1
名字
姓氏
然后在我的代码中,我读取如下值:
<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);
}
<configSections>
<section name="xslTemplateConfiguration" type="Foo.Web.Applications.CustomConfigurationSections.XslTemplateConfiguration"/>
</configSections>
Dictionary参数=新建Dictionary();
foreach(第.ChildNodes节中的XmlNode)
{
添加(n.Name,n.InnerText);
}
有没有更好的储存方法。稍后,我希望能够浏览类似于字典的对象,并能够获取表单字段、查询字符串等的设置
请让我知道我是否可以写得更干净
谢谢您可以使用XML序列化来存储设置并将其直接还原到对象中。它并不完美,但非常容易设置,并为您提供对象保存/恢复功能 拥有此类(属性必须是公共的): 要将其保存到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中注册此部分,如下所示:
<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);
}
<configSections>
<section name="xslTemplateConfiguration" type="Foo.Web.Applications.CustomConfigurationSections.XslTemplateConfiguration"/>
</configSections>
为了读取配置,.NET Framework在命名空间中有许多类。最重要的课程是和。从这些类派生,添加所需的属性并用属性装饰它们。这种方法的优点是类型安全性,可以定义有效值列表,而且.NET Framework会自动从web.config读取、解析和检查所有值。如果您使用的是vs2010,请看这个扩展,它令人惊讶: