Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public ConfigurationNode Clone()
        {
            var clone = new ConfigurationNode(Name);

            foreach (var attribute in Attributes)
            {
                var attributeClone = attribute.Clone();
                clone.Attributes.Add(attributeClone);
            }

            foreach (var childNode in ChildNodes)
            {
                var childNodeClone = childNode.Clone();
                clone.AddChildNode(childNodeClone);
            }

            return(clone);
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static ConfigurationNode Read(TextReader reader)
        {
            var node        = new ConfigurationNode(null);
            var currentNode = node;

            while (reader.Peek() != -1)
            {
                var line = reader.ReadLine();

                if (!string.IsNullOrEmpty(line))
                {
                    if (line[0] == '[')
                    {
                        var index     = line.IndexOf(']');
                        var name      = line.Substring(1, index - 1);
                        var childNode = new ConfigurationNode(name);
                        node.AddChildNode(childNode);
                        currentNode = childNode;
                    }
                    else
                    {
                        var index = line.IndexOf('=');

                        if (index >= 0)
                        {
                            var name   = line.Substring(0, index);
                            var length = line.Length - index - 1;
                            var value  = line.Substring(index + 1, length);
                            currentNode.Attributes.Add(new ConfigurationAttribute(name, value, null));
                        }
                    }
                }
            }

            return(node);
        }
Beispiel #3
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;
                }
            }
        }