共用方式為


ConfigurationElement 類別

定義

表示組態檔中的專案。

public ref class ConfigurationElement
public class ConfigurationElement
type ConfigurationElement = class
Public Class ConfigurationElement
繼承
ConfigurationElement
衍生

範例

下列範例會實作 類別的 ConfigurationElement 數個方法和屬性。 這個範例會 ConfigurationSection 取得 區段的 moduleProvider 物件,並從該區段取得 ConfigurationElementCollection 。 這個範例會逐一查看每個 ConfigurationElement 擷取的集合。

[ModuleServiceMethod(PassThrough = true)]
public ArrayList GetElementCollection()
{
    // Use an ArrayList to transfer objects to the client.
    ArrayList arrayOfConfigPropertyBags = new ArrayList();

    ServerManager serverManager = new ServerManager();
    Configuration administrationConfig = serverManager.GetAdministrationConfiguration();
    ConfigurationSection moduleProvidersSection = 
        administrationConfig.GetSection("moduleProviders");
    ConfigurationElementCollection elementCollection = 
        moduleProvidersSection.GetCollection();
    // If there is a configuration element with the name of TestDemo(Modified), delete it.
    ConfigurationElement elementtoremove = null;
    foreach (ConfigurationElement moduleproviderelement in elementCollection)
    {
        if(moduleproviderelement.Attributes["name"].Value.ToString() == "TestDemo(Modified)")
        {
            elementtoremove = moduleproviderelement;
        }
    }
    if (elementtoremove != null)
    {
        elementCollection.Remove(elementtoremove);
    }
    foreach (ConfigurationElement moduleproviderelement in elementCollection)
    {
        PropertyBag elementBag = new PropertyBag();
        elementBag[ConfigurationDemoGlobals.SchemaName] = 
            moduleproviderelement.Schema.Name;
        elementBag[ConfigurationDemoGlobals.ElementTagName] = 
            moduleproviderelement.ElementTagName;
        ArrayList attributeArrayList = new ArrayList();
        // Loop through all attributes in the element attribute list.
        // Get the attribute name and value.
        foreach (ConfigurationAttribute attribute in moduleproviderelement.Attributes)
        {
            PropertyBag attributeBag = new PropertyBag();
            attributeBag[ConfigurationDemoGlobals.AttributeName] = attribute.Name;
            attributeBag[ConfigurationDemoGlobals.AttributeValue] = attribute.Value;
            // Change the value of the provider name "TestDemo" to "TestDemo(Modified)".
            if (attribute.Value.ToString() == "TestDemo")
            {
                // Use any of the following lines to set the attribute value.
                // attribute.Value = "TestDemo(Modified)";
                moduleproviderelement.SetAttributeValue(
                    "name", "TestDemo(Modified)");
                // moduleproviderelement["name"] = "TestDemo(Modified)";
                // Use any of the following lines to retrieve the attribute value.
                // attributeBag[ConfigurationDemoGlobals.AttributeName] = "name";
                attributeBag[ConfigurationDemoGlobals.AttributeName] =
                    moduleproviderelement.GetAttributeValue("name");
                // attributeBag[ConfigurationDemoGlobals.AttributeName] =
                //    moduleproviderelement["name"];
                // Set the element's lockItem attribute.
                moduleproviderelement.SetMetadata("lockItem", true); // persisted in Administration.config          

            }
            attributeArrayList.Add(attributeBag);
        }
        elementBag[ConfigurationDemoGlobals.AttributeArrayList] = attributeArrayList;
        arrayOfConfigPropertyBags.Add(elementBag);
    }
    // Create a new element. It must have a unique name.
    ConfigurationElement newelement;
    bool newelementexists = false;
    foreach (ConfigurationElement element in elementCollection)
    {
        if (element.Attributes["name"].Value.ToString() == "TestDemo")
        {
            newelementexists = true;
        }
    }
    if (!newelementexists)
    {
        newelement = elementCollection.CreateElement("add");
        newelement.Attributes["name"].Value = "TestDemo";
        newelement.Attributes["Type"].Value = "TestDemo.TestDemoModuleProvider, TestDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=104f78e73dc54601";
        elementCollection.Add(newelement);
    }
    // CommitChanges to persist the changes to Administration.config.
    serverManager.CommitChanges();
    return arrayOfConfigPropertyBags;
}

備註

這是許多組態實體的基類,包括組態區段、集合專案,以及區段中的巢狀專案。

建構函式

ConfigurationElement()

初始化 ConfigurationElement 類別的新執行個體。

屬性

Attributes

取得組態屬性集合,其中包含這個專案的屬性清單。

ChildElements

取得目前專案的所有子專案。

ElementTagName

表示組態檔中的專案。

IsLocallyStored

取得值,指出組態專案是否儲存在特定組態檔中。

Item[String]

取得或設定具有指定名稱的屬性。

Methods

取得組態專案的方法集合。

RawAttributes

表示組態檔中的專案。

Schema

取得目前專案的架構。

方法

Delete()

表示組態檔中的專案。

GetAttribute(String)

ConfigurationAttribute傳回 物件,表示要求的屬性。

GetAttributeValue(String)

傳回指定之屬性的值。

GetChildElement(String)

傳回目前組態專案下且具有指定名稱的子專案。

GetChildElement(String, Type)

傳回目前組態專案下且具有指定名稱和類型的子專案。

GetCollection()

傳回目前組態專案的預設集合。

GetCollection(String)

傳回屬於目前組態專案的所有組態專案。

GetCollection(String, Type)

傳回具有指定名稱和類型的組態專案,且位於目前的組態專案之下。

GetCollection(Type)

傳回具有指定型別且位於目前組態專案底下的組態專案。

GetMetadata(String)

從專案架構傳回中繼資料值。

SetAttributeValue(String, Object)

設定指定屬性的值。

SetMetadata(String, Object)

設定專案架構中的中繼資料值。

適用於