2012-04-20 81 views
0

我打算設置爲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等的設置。

請讓我知道我是否可以寫得更清潔。

感謝

+1

如果你使用的是vs2010,看看這個擴展,其驚人的:http://visualstudiogallery.msdn.microsoft.com/def289f1-377d-4cd0-802a-80c8be8b6758 – asawyer 2012-04-20 22:17:54

回答

1

您可以使用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(); 
} 
0

您可能會考慮的一個選項是創建自定義配置部分,您可以在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; 
    } 
} 
0

對於閱讀配置,.NET的Fra mework在System.Configuration命名空間中有很多類。最重要的課程是ConfigurationSectionConfigurationElement。從這些類派生,添加你想要的屬性並用ConfigurationProperty屬性來修飾它們。這種方法的好處是類型安全,可以定義有效的事實和事實,.NET Framework框架將自動完成所有的讀取,解析和檢查web.config中的值。