【语言-c#】获取程序配置文件指定节点的属性

 一、配置文件示例



  
    
  

 二、调用示例

public void Debug()
{
    string version = "";//此应用程序支持的运行库版本
    string sku = "";//此应用程序支持的 .NET Framework 版本
    version = myConfig.GetAttribute("configuration/startup/supportedRuntime", "version");
    sku = myConfig.GetAttribute("configuration/startup/supportedRuntime", "sku").GetValue();
    // version="v4.0"
    // sku="v4.5.1"
}

三、扩展方法

namespace System
{
    public static class xzSystem
    {
        /// 
        /// name=value
        /// 
        /// 
        /// 
        /// 
        public static string GetName(this string src, string linkChar = "=")
        {
            string ret = "";
            try
            {
                if (string.IsNullOrEmpty(src)) return src;

                if (src.Contains(linkChar) == false) return src;

                int index = src.IndexOf(linkChar);
                if (index < 0) return src;
                else return src.Substring(0, index);
            }
            catch (Exception)
            {

            }
            return ret;
        }
        /// 
        /// name=value
        /// 
        /// 
        /// 
        /// 
        public static string GetValue(this string src, string linkChar = "=")
        {
            string ret = "";
            try
            {
                if (string.IsNullOrEmpty(src)) return src;

                if (src.Contains(linkChar) == false) return src;

                int index = src.IndexOf(linkChar);
                if (index < 0) return src;
                else return src.Substring(index + linkChar.Length, src.Length - index - linkChar.Length);
            }
            catch (Exception)
            {

            }
            return ret;
        }
    }
}

 

四、自定义实现方法 

namespace xzSpace
{
    public class myConfig
    {
        /// 
        /// 获取指定节点所有属性
        /// 
        /// 指定节点路径
        /// 指定属性名称
        /// 
        public static System.Collections.Generic.List GetAttributes(string xpath = "configuration/startup/supportedRuntime", string attributeName = "sku")
        {
            System.Collections.Generic.List ret = new System.Collections.Generic.List();
            try
            {
                System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                doc.Load(config.FilePath);
                //root = doc.DocumentElement;
               System.Xml.XmlNodeList listNodes = null;
                listNodes = doc.SelectNodes(xpath);
                foreach (System.Xml.XmlNode node in listNodes)
                {
                    if (node == null) continue;
                    if (node.GetType() != typeof(System.Xml.XmlElement)) continue;

                    foreach (System.Xml.XmlAttribute xa in node.Attributes)
                    {
                        if (xa.Name == attributeName)
                        {
                            ret.Add(xa.Value);
                        }
                    }
                }
                doc.RemoveAll();
            }
            catch (System.Exception)
            {

            }
            return ret;
        }

        /// 
        /// 获取指定节点属性
        /// 
        /// 指定节点路径
        /// 指定节点属性
        /// 
        public static string GetAttribute(string xpath = "configuration/startup/supportedRuntime", string attributeName = "sku")
        {
            string ret = null;
            try
            {
                System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                doc.Load(config.FilePath);
                System.Xml.XmlNodeList listNodes = null;
                listNodes = doc.SelectNodes(xpath);
                foreach (System.Xml.XmlNode node in listNodes)
                {
                    if (node == null) continue;
                    if (node.GetType() != typeof(System.Xml.XmlElement)) continue;

                    foreach (System.Xml.XmlAttribute xa in node.Attributes)
                    {
                        if (xa.Name == attributeName)
                        {
                            ret=xa.Value;
                        }
                    }
                }
                doc.RemoveAll();
            }
            catch (System.Exception)
            {
            }
            return ret;
        }
    }
}

 

你可能感兴趣的:(语言-c#)