Esempio n. 1
0
        /// <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);
        }
Esempio n. 2
0
        /// <summary>
        /// set the node value by xml xpath
        /// </summary>
        /// <param name="xpath">xpath string</param>
        /// <param name="nodeValue">xml node value</param>
        /// <param name="xmlFullPath">xml file path</param>
        /// <param name="useCache">is use the cache</param>
        public static void SetNodeValueByXPath(string xpath, string nodeValue, string xmlFullPath = null, bool useCache = true)
        {
            if (string.IsNullOrEmpty(xpath))
            {
                LogTool.Info("xpath is not defined");
                return;
            }
            string xmlPath = string.IsNullOrEmpty(xmlFullPath.Trim()) ? _defaultXmlPath : xmlFullPath;

            if (!File.Exists(xmlPath))
            {
                LogTool.Info("xml is not exist");
                return;
            }
            if (useCache)
            {
                StringBuilder sbCombine = new StringBuilder();
                sbCombine.Append(xpath);
                sbCombine.Append(xmlFullPath);
                string cacheKey = sbCombine.ToString().GetHashCode().ToString();
                CacheTool.SetCache(cacheKey, nodeValue, 30);
            }
            SetValueByXPath(xpath, xmlPath, nodeValue);
        }