1

願わくば、この問題をこのサイトのブレイン トラストに提示して、誰かが私の間違いに気付くことを願っています。

私は、さまざまな内部クラスのプロパティにある情報を使用して、電子メール テキストを「メール マージ」する必要があるプロジェクトに取り組んでいます。メール本文に見られる一般的な記号は、「{メンバー名}、{携帯電話} など」のようになります。

web.config の ConfigurationSection を使用して、シンボルとそれらが見つかるクラスを定義したいと思います。これが私の提案する構成セクションです。

<EmailSymbols>
    <SymbolClasses>

        <SymbolClass name="OHMember">
            <Symbol name="Member Name" template="{0} {1}">
                <add index="0" value="SMFirstName" />
                <add index="1" value="SMLastName" />
            </Symbol>
            <Symbol name="Phone" template="{0}">
                <add index="0" value="SMPhone" />
            </Symbol>
        </SymbolClass>

        <SymbolClass name="Form">
            <Symbol name="Contact Name" dataname="ContactName" />
        </SymbolClass>

    </SymbolClasses>
</EmailSymbols>

...そして私がそれを解析しようとしているコード:

public class EmailSymbols : ConfigurationSection {

    [ConfigurationProperty("SymbolClasses", IsRequired = true)]
    public SymbolClassCollection SymbolClasses {
        get {
            return this["SymbolClasses"] as SymbolClassCollection;
        }
    }
}

[ConfigurationCollection(typeof(SymbolClass), AddItemName = "SymbolClass")]
public class SymbolClassCollection : ConfigurationElementCollection {

    protected override ConfigurationElement CreateNewElement() {
        return new SymbolClass();
    }

    protected override object GetElementKey(ConfigurationElement element) {
        return ((SymbolClass)element).Name;
    }
}

[ConfigurationCollection(typeof(Symbol), AddItemName = "Symbol")]
public class SymbolClass : ConfigurationElementCollection {

    [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
    public String Name {
        get {
            return this["name"] as String;
        }
    }

    protected override ConfigurationElement CreateNewElement() {
        return new Symbol();
    }

    protected override object GetElementKey(ConfigurationElement element) {
        return ((Symbol)element).Name;
    }
}

[ConfigurationCollection(typeof(TemplateValue), AddItemName = "add")]
public class Symbol : ConfigurationElementCollection {

    [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
    public String Name {
        get {
            return this["name"] as String;
        }
    }

    [ConfigurationProperty("template", IsRequired = false)]
    public String Template {
        get {
            return this["template"] as String;
        }
    }

    [ConfigurationProperty("dataname", IsRequired = false)]
    public String DataName {
        get {
            return this["dataname"] as String;
        }
    }

    protected override ConfigurationElement CreateNewElement() {
        return new TemplateValue();
    }

    protected override object GetElementKey(ConfigurationElement element) {
        return ((TemplateValue)element).Index;
    }
}

public class TemplateValue : ConfigurationElement {

    [ConfigurationProperty("index", IsRequired = false, IsKey = true)]
    public Int32 Index {
        get {
            return this["index"] == null ? -1 : Convert.ToInt32(this["index"]);
        }
    }

    [ConfigurationProperty("value", IsRequired = false)]
    public String Value {
        get {
            return this["value"] as String;
        }
    }
}

このステートメントでセクションを解析すると: symbols = ConfigurationManager.GetSection("EmailSymbols") as EmailSymbols;

次のエラー メッセージが表示されます:「要素 'Symbol' を認識できません。」

これは単に、.NET の領域であり、私が自分のやり方を知らないだけです。誰でもできる助けをいただければ幸いです。

私の XML 定義は理にかなっており、正しい形式になっていますか? それぞれが Symbol のコレクションを含み、それぞれが TemplateValue のコレクションを含む SymbolClass のコレクションが必要です。

繰り返しますが、あなたの助けに感謝します。

よろしく、 ジミー

4

1 に答える 1