码迷,mamicode.com
首页 > 其他好文 > 详细

读取XML文件中获取特定值

时间:2019-05-24 10:32:27      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:select   tac   col   root   path   attr   xml文件   获取   key   

        /// <summary>
        /// 获取子系统驱动获取DCM配置文件的特定值
        /// </summary>
        /// <param name="configurationFile"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string ReadValueByKey(ConfigurationFile configurationFile, string key)
        {
            string value = string.Empty;
            string filename = string.Empty;
            if (configurationFile.ToString() == ConfigurationFile.AppXml.ToString())
            {
                //string exeroot = "F:\\DCM\\DLAX.DataCollectorMiddleware\\DLAX.DataCollectorMiddleware\\bin\\Debug\\DeviceLib\\视频子系统\\北京电科院\\BJDKY\\Start.exe";
                string exeroot = System.Windows.Forms.Application.ExecutablePath;
                if (exeroot.IndexOf("DeviceLib") > 0)
                {
                    string fileroot = exeroot.Substring(0, exeroot.IndexOf("DeviceLib"));
                    filename = fileroot + "dcm.xml";
                }

                else
                {
                    filename = System.AppDomain.CurrentDomain.BaseDirectory + "dcm.xml";
                }
                    
            }

            XmlDocument doc = new XmlDocument();
            try
            {
                doc.Load(filename); //加载配置文件
            }
            catch (Exception ex)
            {
                return null;
            }
            XmlNode node = doc.SelectSingleNode("//appSettings");   //得到[appSettings]节点

            ////得到[appSettings]节点中关于Key的子节点
            XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key=‘" + key + "‘]");

            if (element != null)
            {
                value = element.GetAttribute("value");
            }

            return value;
        }

读取XML文件中获取特定值

标签:select   tac   col   root   path   attr   xml文件   获取   key   

原文地址:https://www.cnblogs.com/yuesebote/p/10916469.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!