ConfigurationElement Classe

Définition

Représente un élément dans un fichier de configuration.

public ref class ConfigurationElement
public class ConfigurationElement
type ConfigurationElement = class
Public Class ConfigurationElement
Héritage
ConfigurationElement
Dérivé

Exemples

L’exemple suivant implémente plusieurs méthodes et propriétés de la ConfigurationElement classe . Cet exemple obtient un ConfigurationSection objet pour la moduleProvider section et obtient un ConfigurationElementCollection à partir de cette section. Cet exemple itère dans chacune ConfigurationElement des collections récupérées.

[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;
}

Remarques

Il s’agit de la classe de base pour de nombreuses entités de configuration, notamment les sections de configuration, les entrées de collection et les éléments imbriqués dans une section.

Constructeurs

ConfigurationElement()

Initialise une nouvelle instance de la classe ConfigurationElement.

Propriétés

Attributes

Obtient une collection d’attributs de configuration qui contient la liste des attributs de cet élément.

ChildElements

Obtient tous les éléments enfants de l’élément actuel.

ElementTagName

Représente un élément dans un fichier de configuration.

IsLocallyStored

Obtient une valeur indiquant si l’élément de configuration est stocké dans un fichier de configuration particulier.

Item[String]

Obtient ou définit un attribut portant le nom spécifié.

Methods

Obtient une collection de méthodes pour l’élément de configuration.

RawAttributes

Représente un élément dans un fichier de configuration.

Schema

Obtient le schéma de l’élément actif.

Méthodes

Delete()

Représente un élément dans un fichier de configuration.

GetAttribute(String)

Renvoie un ConfigurationAttribute objet qui représente l’attribut demandé.

GetAttributeValue(String)

Retourne la valeur de l'attribut spécifié.

GetChildElement(String)

Retourne un élément enfant qui se trouve sous l’élément de configuration actuel et qui a le nom spécifié.

GetChildElement(String, Type)

Retourne un élément enfant qui se trouve sous l’élément de configuration actuel et qui a le nom et le type spécifiés.

GetCollection()

Retourne la collection par défaut de l’élément de configuration actuel.

GetCollection(String)

Retourne tous les éléments de configuration qui appartiennent à l’élément de configuration actuel.

GetCollection(String, Type)

Retourne l’élément de configuration qui a le nom et le type spécifiés et qui se trouve sous l’élément de configuration actuel.

GetCollection(Type)

Retourne l’élément de configuration qui a le type spécifié et se trouve sous l’élément de configuration actuel.

GetMetadata(String)

Retourne les valeurs de métadonnées du schéma d’élément.

SetAttributeValue(String, Object)

Définit la valeur de l'attribut spécifié.

SetMetadata(String, Object)

Définit les valeurs de métadonnées du schéma d’élément.

S’applique à