0

FormFields、QueryStringパラメーターなどの構成キーを設定する予定です。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);
}

これを保存するより良い方法はありますか?後で、オブジェクトのような辞書を調べて、FormFields、Querystringsなどの設定を取得できるようにしたいと思います。

もっときれいに書けたら教えてください。

ありがとう

4

3 に答える 3

1

XMLシリアル化を使用して、設定を保存し、オブジェクトに直接復元できます。完璧ではありませんが、セットアップが非常に簡単で、オブジェクトの保存/復元が可能です。

このクラスがあります(プロパティはパブリックである必要があります):

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();
}
于 2012-04-20T22:38:08.857 に答える
0

検討する可能性のあるオプションの1つは、に登録できるカスタム構成セクションを作成することです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;
    }
}
于 2012-04-20T23:23:56.130 に答える
0

構成を読み取るために、.NETFrameworkにはSystem.Configuration名前空間に多くのクラスがあります。最も重要なクラスは、ConfigurationSectionConfigurationElementです。これらのクラスから派生し、必要なプロパティを追加して、ConfigurationProperty属性で装飾します。このアプローチの利点は、型の安全性、有効なリストを定義する可能性、および.NET Frameworkフレームワークが、web.configからの値の読み取り、解析、およびチェックをすべて自動的に行うことです。

于 2012-04-20T23:24:32.030 に答える