码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2014-07-16 23:19:18      阅读:316      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   文件   art   io   

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

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

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

标签:style   blog   color   文件   art   io   

原文地址:http://www.cnblogs.com/yuyuanfeng/p/3811472.html

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