/// <summary> /// 从Xml字符串中生成 <see cref="IOption"/> /// </summary> /// <param name="xmlString">Xml字符串</param> /// <returns><see cref="IOption"/></returns> public static IOption CreateFromXmlString(string xmlString) { XmlNode xmlNode = LoadXmlNodeFromString(xmlString, "/"); if (xmlNode is XmlDocument) { xmlNode = ((XmlDocument)xmlNode).DocumentElement; } return(XmlOption.Create(null, xmlNode, true, null, null)); }
/// <summary> /// 从Xml文件中生成 <see cref="IOption"/> /// </summary> /// <param name="xmlFileName">Xml文件</param> /// <returns><see cref="IOption"/></returns> public static IOption CreateFromXmlFile(string xmlFileName) { return(XmlOption.Create(xmlFileName)); }
/// <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; }
/// <summary> /// 从 <see cref="XmlNode"/> 生成 <see cref="IOption"/> /// </summary> /// <param name="xmlNode"><see cref="XmlNode"/></param> /// <returns><see cref="IOption"/></returns> public static IOption CreateFromXmlNode(XmlNode xmlNode) { return(XmlOption.Create(null, xmlNode, true, null, null)); }
/// <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); }