.Net常用技巧_读取XML某节点例子

时间:2014-07-16 23:19:18   收藏:0   阅读:316

注:此例子只是自己在代码中为了读某固定的几个值,写的有点死,所以另作他用的时候请自行修改或扩充

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace MyTool
{
    public class GetConfigValue
    {
        /// <summary>
        /// 读取应用程序列当前配置文件,指定节点的值
        /// </summary>
        /// <param name="AppKey"></param>
        /// <returns></returns>
        public static string Get_ConfigValue(string configFileName, string AppKey)
        {
            try
            {
                System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
                xDoc.Load(System.Windows.Forms.Application.StartupPath + "\\" + configFileName);

                System.Xml.XmlNode xNode;
                System.Xml.XmlElement xElem1;

                xNode = xDoc.SelectSingleNode("//appSettings");

                xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key=‘" + AppKey + "‘]");
                //xElem1 = (XmlElement)xNode.SelectSingleNode("//add key=‘" + AppKey + "‘");

                if (xElem1 != null)
                {
               string val = xElem1.GetAttribute("value");

                     return val;
                }
                else
                {
                    return "";
                }
            }
            catch//(Exception ex)
            {
                return "";
            }
        }

    }
}

 

.Net常用技巧_读取XML某节点例子,布布扣,bubuko.com

评论(0)
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!