Esempio n. 1
0
        private void Read(ConfigurationNode node, StringCollection fileNames)
        {
            var name      = node.Name;
            var endOfNode = _xmlReader.IsEmptyElement;

            if (name != null)
            {
                var hasNext = _xmlReader.MoveToFirstAttribute();

                while (hasNext)
                {
                    var attributeName  = _xmlReader.Name;
                    var attributeValue = _xmlReader.Value;

                    if (attributeName == "name")
                    {
                    }
                    else if (attributeName == "description")
                    {
                        node.Description = attributeValue;
                    }
                    else
                    {
                        var attribute = new ConfigurationAttribute(attributeName, attributeValue, null);
                        node.Attributes.Add(attribute);
                    }

                    hasNext = _xmlReader.MoveToNextAttribute();
                }
            }

            while (!endOfNode && _xmlReader.Read())
            {
                switch (_xmlReader.NodeType)
                {
                case XmlNodeType.Element:
                {
                    var elementName = _xmlReader.Name;

                    switch (elementName)
                    {
                    case ConfigurationElementName.Attribute:
                        ReadAttribute(node);
                        break;

                    case ConfigurationElementName.Node:
                    {
                        var nodeName  = _xmlReader.GetAttribute("name");
                        var childNode = new ConfigurationNode(nodeName);
                        node.AddChildNode(childNode);
                        Read(childNode, fileNames);
                    }

                    break;

                    case "include":
                    {
                        var fileName = _xmlReader.GetAttribute("fileName");
                        fileName = Environment.ExpandEnvironmentVariables(fileName);

                        var reader2     = new ConfigurationReader();
                        var includeNode = reader2.Read(fileName, _sectionName, fileNames);
                        node.Attributes.Add(includeNode.Attributes);

                        foreach (var childNode in includeNode.ChildNodes)
                        {
                            node.AddChildNode(childNode);
                        }

                        if (_enableFileSystemWatcher && !fileNames.Contains(fileName))
                        {
                            fileNames.Add(fileName);
                        }
                    }

                    break;

                    default:
                    {
                        var nodeName  = XmlConvert.DecodeName(elementName);
                        var childNode = new ConfigurationNode(nodeName);
                        node.AddChildNode(childNode);
                        Read(childNode, fileNames);
                    }

                    break;
                    }
                }

                break;

                case XmlNodeType.EndElement:
                    endOfNode = true;
                    break;

                default:
                    break;
                }
            }
        }
        public ConfigurationAttribute Clone()
        {
            var clone = new ConfigurationAttribute(Name, Value, Description);

            return(clone);
        }
Esempio n. 3
0
        private void ReadAttribute(ConfigurationNode node)
        {
            ConfigurationAttribute attribute = null;
            var    name  = _xmlReader["name"];
            object value = null;

            try
            {
                if (name == null)
                {
                    AddError(ErrorType.Warning, "name attribute not found", null);
                }

                var typeName = _xmlReader["type"];
                var type     = GetType(typeName);

                if (type != null)
                {
                    var isNullStr   = _xmlReader["isNull"];
                    var isNull      = false;
                    var description = _xmlReader["description"];

                    if (isNullStr != null)
                    {
                        try
                        {
                            isNull = bool.Parse(isNullStr);
                        }
                        catch (Exception e)
                        {
                            AddError(ErrorType.Error, "Error parsing isNull attribute.", e);
                        }
                    }

                    if (!isNull)
                    {
                        var valueStr = _xmlReader["value"];

                        try
                        {
                            if (valueStr != null)
                            {
                                value = Convert.ChangeType(valueStr, type, _formatProvider);
                            }
                            else
                            {
                                value = ReadAttributeValue(type);
                            }
                        }
                        catch (Exception e)
                        {
                            AddError(ErrorType.Error, "Reading attribute value failed.", e);
                        }
                    }

                    attribute = new ConfigurationAttribute(name, value, description);
                }
                else
                {
                    AddError(ErrorType.Warning, "Parsing attribute type failed.", null);
                }
            }
            catch (Exception e)
            {
                AddError(ErrorType.Error, "Reading attribute failed.", e);
            }

            if (attribute != null)
            {
                try
                {
                    node.Attributes.Add(attribute);
                }
                catch (Exception e)
                {
                    AddError(ErrorType.Error, "Adding attribute to node failed.", e);
                }
            }
        }