首页 > 代码库 > .Net常用技巧_读取XML某节点例子

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

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

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 "";            }        }    }}