超高性能的超级简单的C#程序配置项

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>  
        /// 返回XMl文件指定元素的指定属性值  
        /// </summary>  
        /// <param name="xmlElement">指定元素</param>  
        /// <param name="xmlAttribute">指定属性</param>  
        /// <param name="xmlpath">读取的xml文件名字</param>  
        /// <param name="reloadXml">是否重新加载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>  
        /// 设置XMl文件指定元素的指定属性的值  
        /// </summary>  
        /// <param name="xmlElement">指定元素</param>  
        /// <param name="xmlAttribute">指定属性</param>  
        /// <param name="xmlValue">指定值</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);
        }
    }

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页