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)

设置元素架构中的元数据值。

适用于