Super high performance super simple C# program configuration item

using System.IO;
using System.Linq;
using System.Xml.Linq;

 public class ConfigSetting

    {
        private static XDocument xmlDoc = null;
        private static string currentPath = @"\Settings\System.xml";


        /// <summary>  
        /// Returns the specified attribute value of the specified element in the XMl file  
        /// </summary>  
        // / <param name="xmlElement">specified element</param>  
        /// <param name="xmlAttribute">specified attribute</param>  
        /// <param name="xmlpath">read xml file name< /param>  
        /// <param name="reloadXml">Whether to reload XML</param>  
        /// <returns></returns>  
        public static string GetXmlValue(string xmlElement, string xmlAttribute = "value", bool reloadXml = false)
        {
            if (xmlDoc == null || reloadXml)
            {
                xmlDoc = XDocument.Load(currentPath);
            }
            var results = from c in xmlDoc.Descendants(xmlElement)
                          select c;
            string s = "";
            foreach (var result in results)
            {
                s = result.Attribute(xmlAttribute).Value. ToString();
            }
            return s;
        }


        /// <summary>  
        /// Set the value of the specified attribute of the specified element in the XMl file  
        /// </summary>  
        /// <param name="xmlElement">specified element<// param>  
        /// <param name="xmlAttribute">specified attribute</param>  
        /// <   param name="xmlValue">specified value</param>
        public static void SetXmlValue(string xmlElement,  string xmlValue, string xmlAttribute="value", string xmlRootName = "Application", bool reloadXml = false)
        {
            if (xmlDoc == null || reloadXml)
            {
                xmlDoc = XDocument.Load(currentPath);
            }
            xmlDoc.Element(xmlRootName).Element(xmlElement).Attribute(xmlAttribute).SetValue(xmlValue);
            xmlDoc.Save(currentPath);
        }
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324495962&siteId=291194637
Recommended