Пример #1
0
        /// <summary>
        /// 创建配置节
        /// </summary>
        /// <param name="parent">父配置节</param>
        /// <param name="xmlNode">XML节</param>
        /// <param name="readonly">是否只读</param>
        /// <param name="searchPath">XML搜索目录列表</param>
        /// <param name="configFiles">如果有子配置文件,则添加到此列表</param>
        internal static XmlConfigSetting Create(XmlConfigSetting parent, XmlNode xmlNode, bool @readonly, string[] searchPath, List<string> configFiles)
        {
            if (xmlNode.NodeType != XmlNodeType.Element) {
                throw new ConfigException("解析到非法元素");
            }

            if (searchPath == null || searchPath.Length <= 0) {
                searchPath = ConfigHelper.ConfigFileDefaultSearchPath;
            }
            XmlConfigSetting setting = new XmlConfigSetting();
            setting.parent = parent;
            setting.ReadOnly = @readonly;
            setting.property = new XmlSettingProperty(xmlNode, @readonly);
            setting.childSettings = new ConfigSettingCollection(true);
            setting.operatorSettings = new ConfigSettingCollection(false);
            setting.value = new XmlSettingValue(xmlNode, @readonly);
            setting.SettingName = setting.Name;
            ConfigSettingOperator settingOperator = setting.SettingOperator;
            if (settingOperator != 0 && settingOperator != ConfigSettingOperator.Clear) {
                string newName = setting.Property.TryGetPropertyValue(ConfigNamePropertyName);
                if (string.IsNullOrEmpty(newName)) {
                    throw new ConfigException("配置命令未定义属性:" + ConfigNamePropertyName);
                }
                setting.value.SetName(newName);
            }

            foreach (XmlNode node in xmlNode.ChildNodes) {
                if (node.NodeType != XmlNodeType.Element) {
                    continue;
                }
                XmlConfigSetting childSetting = Create(setting, node, @readonly, searchPath, configFiles);
                setting.operatorSettings.Add(childSetting);
            }
            Compile(setting, setting.operatorSettings);

            string configFile = setting.ConfigFile;
            string configNode = setting.ConfigNode;
            if (!string.IsNullOrEmpty(configFile)) {
                configFile = ConfigHelper.SearchConfigFile(configFile, searchPath);
                if (!string.IsNullOrEmpty(configFile)) {
                    if (string.IsNullOrEmpty(configNode)) {
                        configNode = "/";
                    }
                    XmlNode newNode = ConfigHelper.LoadXmlNodeFromFile(configFile, configNode, false);
                    if (newNode != null) {
                        if (configFiles != null) {
                            configFiles.Add(configFile);
                        }
                        setting.Merge(Create(parent, newNode, @readonly, searchPath, configFiles));
                    }
                }
            }
            return setting;
        }
Пример #2
0
        /// <summary>
        /// 创建配置节
        /// </summary>
        /// <param name="parent">父配置节</param>
        /// <param name="xmlNode">XML节</param>
        /// <param name="readonly">是否只读</param>
        /// <param name="searchPath">XML搜索目录列表</param>
        /// <param name="configFiles">如果有子配置文件,则添加到此列表</param>
        internal static XmlConfigSetting Create(XmlConfigSetting parent, XmlNode xmlNode, bool @readonly, string[] searchPath, List <string> configFiles)
        {
            if (xmlNode.NodeType != XmlNodeType.Element)
            {
                throw new ConfigException("解析到非法元素");
            }

            if (searchPath == null || searchPath.Length <= 0)
            {
                searchPath = ConfigHelper.ConfigFileDefaultSearchPath;
            }
            XmlConfigSetting setting = new XmlConfigSetting();

            setting.parent           = parent;
            setting.ReadOnly         = @readonly;
            setting.property         = new XmlSettingProperty(xmlNode, @readonly);
            setting.childSettings    = new ConfigSettingCollection(true);
            setting.operatorSettings = new ConfigSettingCollection(false);
            setting.value            = new XmlSettingValue(xmlNode, @readonly);
            setting.SettingName      = setting.Name;
            ConfigSettingOperator settingOperator = setting.SettingOperator;

            if (settingOperator != 0 && settingOperator != ConfigSettingOperator.Clear)
            {
                string newName = setting.Property.TryGetPropertyValue(ConfigNamePropertyName);
                if (string.IsNullOrEmpty(newName))
                {
                    throw new ConfigException("配置命令未定义属性:" + ConfigNamePropertyName);
                }
                setting.value.SetName(newName);
            }

            foreach (XmlNode node in xmlNode.ChildNodes)
            {
                if (node.NodeType != XmlNodeType.Element)
                {
                    continue;
                }
                XmlConfigSetting childSetting = Create(setting, node, @readonly, searchPath, configFiles);
                setting.operatorSettings.Add(childSetting);
            }
            Compile(setting, setting.operatorSettings);

            string configFile = setting.ConfigFile;
            string configNode = setting.ConfigNode;

            if (!string.IsNullOrEmpty(configFile))
            {
                configFile = ConfigHelper.SearchConfigFile(configFile, searchPath);
                if (!string.IsNullOrEmpty(configFile))
                {
                    if (string.IsNullOrEmpty(configNode))
                    {
                        configNode = "/";
                    }
                    XmlNode newNode = ConfigHelper.LoadXmlNodeFromFile(configFile, configNode, false);
                    if (newNode != null)
                    {
                        if (configFiles != null)
                        {
                            configFiles.Add(configFile);
                        }
                        setting.Merge(Create(parent, newNode, @readonly, searchPath, configFiles));
                    }
                }
            }
            return(setting);
        }