/// <summary> /// 创建选项 /// </summary> /// <param name="fileName">XML文件</param> /// <param name="readonly">是否只读</param> /// <param name="searchPath">XML搜索目录列表</param> /// <param name="configFiles">如果有子选项文件,则添加到此列表</param> /// <returns>选项节</returns> internal static XmlOption Create(string fileName, bool @readonly, string[] searchPath, List <string> configFiles) { fileName = OptionHelper.SearchOptionFile(fileName, searchPath); if (string.IsNullOrEmpty(fileName)) { return(null); } XmlNode xmlNode = OptionHelper.LoadXmlNodeFromFile(fileName, "/", false); if (xmlNode == null) { return(null); } if (searchPath == null || searchPath.Length <= 0) { searchPath = OptionHelper.OptionFileDefaultSearchPath; } string[] newSearchPath = new string[searchPath.Length + 1]; newSearchPath[0] = Path.GetDirectoryName(fileName); searchPath.CopyTo(newSearchPath, 1); return(Create(null, xmlNode, @readonly, newSearchPath, configFiles)); }
/// <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 XmlOption Create(XmlOption parent, XmlNode xmlNode, bool @readonly, string[] searchPath, List <string> configFiles) { if (xmlNode.NodeType != XmlNodeType.Element) { throw new OptionException("解析到非法元素"); } if (searchPath == null || searchPath.Length <= 0) { searchPath = OptionHelper.OptionFileDefaultSearchPath; } XmlOption setting = new XmlOption(); setting.parent = parent; setting.ReadOnly = @readonly; setting.property = new XmlOptionSection(xmlNode, @readonly); setting.childSettings = new OptionCollection(true); setting.operatorSettings = new OptionCollection(false); setting.value = new XmlOptionValue(xmlNode, @readonly); setting.SettingName = setting.Name; OptionOperatorEnum settingOperator = setting.SettingOperator; if (settingOperator != 0 && settingOperator != OptionOperatorEnum.Clear) { string newName = setting.Property.TryGetPropertyValue(OptionNamePropertyName); if (string.IsNullOrEmpty(newName)) { throw new OptionException("选项命令未定义属性:" + OptionNamePropertyName); } setting.value.SetName(newName); } foreach (XmlNode node in xmlNode.ChildNodes) { if (node.NodeType != XmlNodeType.Element) { continue; } XmlOption childSetting = Create(setting, node, @readonly, searchPath, configFiles); setting.operatorSettings.Add(childSetting); } Compile(setting, setting.operatorSettings); string configFile = setting.OptionFile; string configNode = setting.OptionNode; if (!string.IsNullOrEmpty(configFile)) { configFile = OptionHelper.SearchOptionFile(configFile, searchPath); if (!string.IsNullOrEmpty(configFile)) { if (string.IsNullOrEmpty(configNode)) { configNode = "/"; } XmlNode newNode = OptionHelper.LoadXmlNodeFromFile(configFile, configNode, false); if (newNode != null) { if (configFiles != null) { configFiles.Add(configFile); } setting.Merge(Create(parent, newNode, @readonly, searchPath, configFiles)); } } } return(setting); }