/// <summary> /// set the node value by Xml xpath with out check /// </summary> /// <param name="xpath">xpath string</param> /// <param name="xmlPath">xml file path</param> /// <param name="nodeValue">xml node value</param> /// <returns>is set value succeed</returns> private static bool SetValueByXPath(string xpath, string xmlPath, string nodeValue) { bool res = false; XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.Load(xmlPath); if (xmlDoc == null) { return(res); } XmlNode node = xmlDoc.SelectSingleNode(xpath); if (node == null) { return(res); } node.InnerText = nodeValue; res = true; } catch (Exception exLoad) { LogTool.Error(exLoad); } return(res); }
/// <summary> /// get node value by Xml xpath from the xml source text with out check /// </summary> /// <param name="xpath">xml path</param> /// <param name="xmlSourceText">xml source text</param> /// <returns>node value string</returns> private static string GetValueByXPathInSource(string xpath, string xmlSourceText) { string res = string.Empty; XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.LoadXml(xmlSourceText); if (xmlDoc == null) { res = string.Empty; } XmlNode node = xmlDoc.SelectSingleNode(xpath); if (node == null) { res = string.Empty; } res = node.InnerText.Replace("\t", "").Replace("\r", "").Replace("\n", "").Trim(); } catch (Exception exLoad) { LogTool.Error(exLoad); } return(res); }
/// <summary> /// convert key-value pair to model /// </summary> /// <typeparam name="T">model type</typeparam> /// <param name="source">key-value pair</param> /// <returns>model</returns> public static T GetModel <T>(Dictionary <string, object> source) where T : class { if (source == null || source.Count <= 0) { return(null); } Type type = typeof(T); PropertyInfo[] propertyInfos = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); if (propertyInfos == null || propertyInfos.Length <= 0) { return(null); } T res = Activator.CreateInstance <T>(); try { foreach (PropertyInfo item in propertyInfos) { if (item == null || string.IsNullOrWhiteSpace(item.Name.ToLower()) || !source.Keys.Contains(item.Name.ToLower())) { continue; } SetPropertyValue(item, res, source[item.Name.ToLower()]); } } catch (Exception ex) { LogTool.Error(ex); return(null); } return(res.Equals(Activator.CreateInstance <T>()) ? null : res); }
/// <summary> /// check the key exists in memory cache /// </summary> /// <param name="key">key</param> /// <returns>the check result</returns> public static bool Exists(string key) { try { return(CacheEntity.Get(key) == null); } catch (Exception ex) { LogTool.Error(ex); return(false); } }
/// <summary> /// remove the value from the cache by the key /// </summary> /// <param name="key">key</param> public static void RemoveCache(string key) { try { if (Exists(key)) { CacheEntity.Remove(key); } } catch (Exception ex) { LogTool.Error(ex); return; } }
/// <summary> /// get node value from the xml file by xpath /// </summary> /// <param name="xpath">xml xpath</param> /// <param name="targetXml">xml file full path or xml content</param> /// <param name="useCache">is use the cache</param> /// <param name="updateForce">force update the cache</param> /// <returns>node value</returns> public static string GetNodeValueByXPath(string xpath, string targetXml = null, bool useCache = true, bool updateForce = true) { if (string.IsNullOrEmpty(xpath)) { LogTool.Info("xpath is not defined"); return(string.Empty); } targetXml = string.IsNullOrEmpty(targetXml) ? _defaultXmlPath : targetXml; string res = string.Empty; StringBuilder sbCombine = new StringBuilder(); sbCombine.Append(xpath); sbCombine.Append(targetXml); string cacheKey = sbCombine.ToString().GetHashCode().ToString(); try { if (useCache) { if (CacheTool.Exists(cacheKey)) { if (updateForce) { res = GetValueByXPath(xpath, targetXml); CacheTool.SetCache(cacheKey, res, 30); } else { res = ConvertTool.GetString(CacheTool.GetCache(cacheKey)); } } else { res = GetValueByXPath(xpath, targetXml); CacheTool.SetCache(cacheKey, res, 30); } } else { res = GetValueByXPath(xpath, targetXml); } } catch (Exception ex) { LogTool.Error(ex); } return(res); }
/// <summary> /// get cache value /// </summary> /// <param name="key">cache key</param> /// <returns>cache value</returns> public static object GetCache(string key) { if (string.IsNullOrEmpty(key.Trim())) { return(null); } try { return(CacheEntity.Get(key)); } catch (Exception ex) { LogTool.Error(ex); return(null); } }
/// <summary> /// get cache value /// </summary> /// <typeparam name="T">cache value type</typeparam> /// <param name="key">cache key</param> /// <returns>cache value</returns> public static List <T> GetCacheList <T>(string key) where T : class { if (string.IsNullOrEmpty(key.Trim())) { return(null); } try { return(CacheEntity.Get(key) as List <T>); } catch (Exception ex) { LogTool.Error(ex); return(null); } }
/// <summary> /// set cache /// </summary> /// <param name="key">cache key</param> /// <param name="value">cache value</param> /// <param name="minutes">cache expiration</param> /// <returns>set result</returns> public static bool SetCache(string key, object value, int?minutes = 20) { try { RemoveCache(key); if (minutes == null) { CacheEntity.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration); } else { CacheEntity.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, minutes.Value, 0)); } return(true); } catch (Exception ex) { LogTool.Error(ex); return(false); } }
/// <summary> /// compare model property value equal or not /// </summary> /// <typeparam name="T">model type</typeparam> /// <param name="a">value 1</param> /// <param name="b">value 2</param> /// <returns>compare result</returns> public static bool ACompareB <T>(T a, T b) where T : class { if (a == null && b == null) { return(true); } if ((a == null && b != null) || (a != null && b == null)) { return(false); } Type type = typeof(T); PropertyInfo[] propertyInfos = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); if (propertyInfos == null || propertyInfos.Length <= 0) { return(false); } try { foreach (PropertyInfo pi in propertyInfos) { object tmpA = pi.GetValue(a, null); object tmpB = pi.GetValue(b, null); if (CompareValue(tmpA, tmpB, pi)) { continue; } else { return(false); } } return(true); } catch (Exception ex) { LogTool.Error(ex); return(false); } }
/// <summary> /// get node value string by Xml xpath with out check /// </summary> /// <param name="xpath">xpath string</param> /// <param name="targetXml">xml file full path or xml content</param> /// <returns>node value string</returns> private static string GetValueByXPath(string xpath, string targetXml) { string res = string.Empty; XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.Load(targetXml); if (xmlDoc == null) { xmlDoc.LoadXml(targetXml); if (xmlDoc == null) { res = string.Empty; } } XmlNode node = xmlDoc.SelectSingleNode(xpath); if (node == null) { res = string.Empty; } string[] tmp = node.InnerText.Split(new char[] { '\t', '\r', '\n', ' ' }, StringSplitOptions.RemoveEmptyEntries); if (tmp != null && tmp.Length > 0) { res = string.Join(" ", tmp); } else { res = null; } } catch (Exception exLoad) { LogTool.Error(exLoad); } return(res); }
/// <summary> /// object convert to string /// </summary> /// <param name="obj">object</param> /// <param name="timeFormat">datetime format string</param> /// <returns>string</returns> public static string GetString(object obj, string timeFormat = null) { if (obj == null || obj == DBNull.Value) { return(null); } DateTime?tmpDatetime = obj as DateTime?; if (string.IsNullOrWhiteSpace(timeFormat)) { timeFormat = "yyyy-MM-dd HH:mm:ss.fff"; } if (tmpDatetime.HasValue) { if (string.IsNullOrWhiteSpace(timeFormat.Trim())) { return(tmpDatetime.Value.ToString("yyyy-MM-dd HH:mm:ss.fff")); } else { try { return(tmpDatetime.Value.ToString(timeFormat)); } catch (Exception ex) { LogTool.Error(ex); return(tmpDatetime.Value.ToString("yyyy-MM-dd HH:mm:ss.fff")); } } } else { return(obj.ToString()); } }