/// <summary> /// Process the "configSection" xml node section /// </summary> /// <param name="configChildNode"></param> /// <param name="sectionSettings"></param> /// <param name="configSettings"></param> void ProcessConfigSection( XmlNode configChildNode, out ConfigSectionSettings sectionSettings, ConfigurationManagementSettings configSettings ) { // Initialize a new ConfigSectionSettings. sectionSettings = new ConfigSectionSettings(); // Get a collection of all the attributes. XmlAttributeCollection nodeAttributes = configChildNode.Attributes; //#region Remove the known attributes and load the struct values // Remove the name attribute from the node and set its value in ConfigSectionSettings. XmlNode currentAttribute = nodeAttributes.RemoveNamedItem( "name" ); if (currentAttribute != null) sectionSettings.Name = currentAttribute.Value; // Loop through the section components and load them into the ConfigurationManagementSettings. ConfigCacheSettings cacheSettings; ConfigProviderSettings providerSettings; DataProtectionProviderSettings protectionSettings; foreach(XmlNode sectionChildNode in configChildNode.ChildNodes) { switch ( sectionChildNode.Name ) { case "configCache" : ProcessConfigCacheSection( sectionChildNode, out cacheSettings, sectionSettings ); break; case "configProvider" : ProcessConfigProviderSection( sectionChildNode, out providerSettings, sectionSettings ); break; case "protectionProvider" : ProcessProtectionProvider( sectionChildNode, out protectionSettings, sectionSettings ); break; default : break; } } //#endregion // Add the ConfigurationSectionSettings to the sections collection. configSettings.AddConfigurationSection(sectionSettings); }
/// <summary> /// Builds the ConfigurationManagementSettings, ConfigurationProviderSettings and ConfigurationItemsSettings structures based on the configuration file. /// </summary> /// <param name="parent">Composed from the configuration settings in a corresponding parent configuration section.</param> /// <param name="configContext">Provides access to the virtual path for which the configuration section handler computes configuration values. Normally this parameter is reserved and is null.</param> /// <param name="section">The XML node that contains the configuration information to be handled. section provides direct access to the XML contents of the configuration section.</param> /// <returns>The ConfigurationManagementSettings struct built from the configuration settings.</returns> public object Create(object parent,object configContext,XmlNode section) { try { ConfigurationManagementSettings configSettings = new ConfigurationManagementSettings(); // Exit if there is no configuration settings. if (section == null) return configSettings; // Validate the document using a schema XmlValidatingReader vreader = new XmlValidatingReader( new XmlTextReader( new StringReader( section.OuterXml ) ) ); using( Stream xsdFile = Resource.ResourceManager.GetStream( "Microsoft.ApplicationBlocks.ConfigurationManagement.ConfigSchema.xsd" ) ) { using( StreamReader sr = new StreamReader( xsdFile ) ) { vreader.ValidationEventHandler += new ValidationEventHandler( ValidationCallBack ); vreader.Schemas.Add( XmlSchema.Read( new XmlTextReader( sr ), null ) ); vreader.ValidationType = ValidationType.Schema; // Validate the document while (vreader.Read()){} if( !_isValidDocument ) { throw new ConfigurationException( Resource.ResourceManager[ "Res_ExceptionDocumentNotValidated", _schemaErrors ] ); } } } XmlAttribute attr = section.Attributes[ "defaultSection" ]; if( attr != null ) configSettings.DefaultSectionName = attr.Value; //#region Loop through the section components and load them into the ConfigurationManagementSettings ConfigSectionSettings sectionSettings; foreach(XmlNode configChildNode in section.ChildNodes) { if (configChildNode.Name == "configSection" ) { ProcessConfigSection( configChildNode, out sectionSettings, configSettings ); } } //#endregion // Return the ConfigurationManagementSettings loaded with the values from the config file. return configSettings; } catch (Exception exc) { throw new ConfigurationException( Resource.ResourceManager[ "RES_ExceptionLoadingConfiguration" ], exc, section); } }