/// <summary> /// 根据指定的条件获取属性的值. /// </summary> /// <param name="name">属性名称.</param> /// <param name="defaultValue">未找到属性时返回的缺省值.</param> /// <param name="type">属性值检索方式.</param> /// <returns>检索结果.</returns> public string GetString(string name, string defaultValue, XmlParserTypes type) { var target = string.Empty; if (type == XmlParserTypes.Attribute) { Attributes.TryGetValue(name, out target); } else if (type == XmlParserTypes.InnerElement) { Children.TryGetValue(name, out target); } else if (type == XmlParserTypes.Auto) { if (Attributes.TryGetValue(name, out target)) { return(target); } else if (Children.TryGetValue(name, out target)) { return(target); } } else { throw new NotSupportedException("error enum"); } return(target); }
/// <summary> /// 根据指定的条件获取属性的值. /// </summary> /// <typeparam name="T">待分析的类型.</typeparam> /// <param name="name">属性名称.</param> /// <param name="tryParse">字符串尝试分析委托.</param> /// <param name="defaultValue">未找到属性时返回的缺省值.</param> /// <param name="type">属性值检索方式.</param> /// <returns>检索结果.</returns> public T GetValue <T>(string name, TryParse <T> tryParse, T defaultValue, XmlParserTypes type) { var value = GetString(name, null, type); if (string.IsNullOrEmpty(value)) { return(defaultValue); } T result = default(T); if (tryParse(value, out result)) { return(result); } return(defaultValue); }
/// <summary> /// 根据指定的条件获取属性的值. /// </summary> /// <param name="name">属性名称.</param> /// <param name="defaultValue">未找到属性时返回的缺省值.</param> /// <param name="type">属性值检索方式.</param> /// <returns>检索结果.</returns> public int GetInteger(string name, int defaultValue, XmlParserTypes type) { var value = GetString(name, null, type); if (string.IsNullOrEmpty(value)) { return(defaultValue); } int result = 0; if (int.TryParse(value, out result)) { return(result); } return(defaultValue); }