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

 一、配置文件示例

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
  </startup>
</configuration>

 二、调用示例

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
    {
        /// <summary>
        /// <format>name=value</format>
        /// </summary>
        /// <param name="src"></param>
        /// <param name="linkChar"></param>
        /// <returns></returns>
        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;
        }
        /// <summary>
        /// <format>name=value</format>
        /// </summary>
        /// <param name="src"></param>
        /// <param name="linkChar"></param>
        /// <returns></returns>
        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
    {
        /// <summary>
        /// 获取指定节点所有属性
        /// </summary>
        /// <param name="xpath">指定节点路径</param>
        /// <param name="attributeName">指定属性名称</param>
        /// <returns></returns>
        public static System.Collections.Generic.List<string> GetAttributes(string xpath = "configuration/startup/supportedRuntime", string attributeName = "sku")
        {
            System.Collections.Generic.List<string> ret = new System.Collections.Generic.List<string>();
            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;
        }

        /// <summary>
        /// 获取指定节点属性
        /// </summary>
        /// <param name="xpath">指定节点路径</param>
        /// <param name="attributeName">指定节点属性</param>
        /// <returns></returns>
        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;
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值