/// <summary>
		/// 
		/// </summary>
		/// <param name="parent"></param>
		/// <param name="configContext"></param>
		/// <param name="section"></param>
		/// <returns></returns>
		public virtual object Create(object parent, object configContext, XmlNode section)
		{
			object result = parent;
			XmlNode fileAttribute = section.Attributes.RemoveNamedItem("file");
			result = NameValueSectionHandler.CreateStatic(result, section);
			if (fileAttribute != null && fileAttribute.Value.Length != 0)
			{
				string sectionName = fileAttribute.Value;
				IConfigXmlNode configXmlNode = fileAttribute as IConfigXmlNode;
				if (configXmlNode == null)
				{
					return null;
				}
				string sourceFileFullPath = Path.Combine(Path.GetDirectoryName(configXmlNode.Filename), sectionName);
				if (File.Exists(sourceFileFullPath))
				{
					ConfigXmlDocument configXmlDocument = new ConfigXmlDocument();
					try
					{
						configXmlDocument.Load(sourceFileFullPath);
					}
					catch (XmlException e)
					{
						throw new ConfigurationException(e.Message, e, sourceFileFullPath, e.LineNumber);
					}
					if (section.Name != configXmlDocument.DocumentElement.Name)
					{
						throw new ConfigurationException("Config NameValueFile Section: Invalid root", configXmlDocument.DocumentElement);
					}
					result = NameValueSectionHandler.CreateStatic(result, configXmlDocument.DocumentElement);
				}
			}
			return result;
		}
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="configContext"></param>
        /// <param name="section"></param>
        /// <returns></returns>
        public virtual object Create(object parent, object configContext, XmlNode section)
        {
            object  result        = parent;
            XmlNode fileAttribute = section.Attributes.RemoveNamedItem("file");

            result = NameValueSectionHandler.CreateStatic(result, section);
            if (fileAttribute != null && fileAttribute.Value.Length != 0)
            {
                string         sectionName   = fileAttribute.Value;
                IConfigXmlNode configXmlNode = fileAttribute as IConfigXmlNode;
                if (configXmlNode == null)
                {
                    return(null);
                }
                string sourceFileFullPath = Path.Combine(Path.GetDirectoryName(configXmlNode.Filename), sectionName);
                if (File.Exists(sourceFileFullPath))
                {
                    ConfigXmlDocument configXmlDocument = new ConfigXmlDocument();
                    try
                    {
                        configXmlDocument.Load(sourceFileFullPath);
                    }
                    catch (XmlException e)
                    {
                        throw new ConfigurationException(e.Message, e, sourceFileFullPath, e.LineNumber);
                    }
                    if (section.Name != configXmlDocument.DocumentElement.Name)
                    {
                        throw new ConfigurationException("Config NameValueFile Section: Invalid root", configXmlDocument.DocumentElement);
                    }
                    result = NameValueSectionHandler.CreateStatic(result, configXmlDocument.DocumentElement);
                }
            }
            return(result);
        }
Example #3
0
        private object EvaluateRecursive(IConfigurationSectionHandler factory, object config, string[] keys, int iKey, XmlTextReader reader, object context)
        {
            string name  = keys[iKey];
            int    depth = reader.Depth;

            while (reader.Read() && reader.NodeType != XmlNodeType.Element)
            {
                ;
            }

            while (reader.Depth == depth + 1)
            {
                if (reader.Name == name)
                {
                    if (iKey < keys.Length - 1)
                    {
                        config = EvaluateRecursive(factory, config, keys, iKey + 1, reader, context);
                    }
                    else
                    {
                        // Call configuration section handler
                        int line = reader.LineNumber;

                        // Try-catch is necessary to protect from exceptions in user config handlers
                        try
                        {
                            ConfigXmlDocument doc = new ConfigXmlDocument();
                            doc.LoadSingleElement(filename, reader);
                            config = factory.Create(config, context, doc.DocumentElement);
                        }
                        catch (ConfigurationException)
                        {
                            // Bubble ConfigurationExceptions
                            throw;
                        }
                        catch (XmlException)
                        {
                            // Bubble XmlExceptions
                            throw;
                        }
                        catch (Exception ex)
                        {
                            // Wrap all others as ConfigurationExceptions
                            throw new ConfigurationException("Exception in ConfigSectionHandler", ex, filename, line);
                        }
                    }
                    continue;
                }
                StrictSkipToNextElement(reader);
            }
            return(config);
        }
		private object EvaluateRecursive(IConfigurationSectionHandler factory, object config, string[] keys, int iKey, XmlTextReader reader)
		{
			string name = keys[iKey];
			int depth = reader.Depth;
			
			while(reader.Read() && reader.NodeType != XmlNodeType.Element);

			while (reader.Depth == depth + 1)
			{
				if (reader.Name == name)
				{
					if (iKey < keys.Length - 1)
					{
						config = EvaluateRecursive(factory, config, keys, iKey + 1, reader);
					}
					else 
					{
						// Call configuration section handler
						int line = reader.LineNumber;

						// Try-catch is necessary to protect from exceptions in user config handlers
						try
						{
							ConfigXmlDocument doc = new ConfigXmlDocument();
							doc.LoadSingleElement(filename, reader);
							config = factory.Create(config, null, doc.DocumentElement);
						}
						catch(ConfigurationException)
						{
							// Bubble ConfigurationExceptions
							throw;
						}
						catch (XmlException)
						{
							// Bubble XmlExceptions
							throw;
						}
						catch(Exception ex)
						{
							// Wrap all others as ConfigurationExceptions
							throw new ConfigurationException("Exception in ConfigSectionHandler", ex, filename, line);
						}
					}
					continue;
				}
				StrictSkipToNextElement(reader);
			}
			return config;
		}