利用xml轻松读取web.config中的用户自定义节

虽然vs.net2.0为我们提供了

ConfigurationManager.AppSettings["..."];
ConfigurationManager.ConnectionStrings["..."];

这样方便快捷的方法来访问web.config中的AppSettings和ConnectionStrings这二个节的内容

但对于其它节,特别是用户自定义节的访问却并不方便,比如web.config中有这么一段内容

<?xml version="1.0"?>
<configuration>

...
 <system.webServer>
  <validation validateIntegratedModeConfiguration="false" myname="Jimmy"/>
  
  ...
 </system.webServer>
</configuration>


我们要访问system.webServer下的validation节点中的validateIntegratedModeConfiguration或myname的属性值,就远远没有象ConfigurationManager.AppSettings这样来得方便,其实web.config是一个标准的xml,我们完全可以按xml的xpath语句来检索xml中的任何内容,这里我提供了一个示例,为了方便操作xml,把对xml的一些操作方法封装到了一个类里,以下是这个类的主要代码

using System;
using System.Xml;
using System.Configuration;
using System.Data;
using System.Collections;
using System.IO;

namespace JIMMY.TOOLS
{
    
/// <summary>
    
/// Description:XML封装操作类1.04版
    
/// Author:jimmy mail:yjmyzz@126.com Date:2006-10-28
    
/// </summary>

    public class XmlControl
    
{
        
protected string strXmlFile;
        
protected XmlDocument objXmlDoc = new XmlDocument();

        
public XmlControl(string XmlFile, Boolean bOverWrite, string sRoot)
        
{
            
try
            
{
                
//如果覆盖模式,则强行创建一个xml文档
                if (bOverWrite)
                
{
                    objXmlDoc.AppendChild(objXmlDoc.CreateXmlDeclaration(
"1.0""utf-8"null));//设置xml的版本,格式信息
                    objXmlDoc.AppendChild(objXmlDoc.CreateElement("", sRoot, ""));//创建根元素
                    objXmlDoc.Save(XmlFile);//保存
                }

                
else //否则,检查文件是否存在,不存在则创建
                {
                    
if (!(File.Exists(XmlFile)))
                    
{
                        objXmlDoc.AppendChild(objXmlDoc.CreateXmlDeclaration(
"1.0""utf-8"null));
                        objXmlDoc.AppendChild(objXmlDoc.CreateElement(
"", sRoot, ""));
                        objXmlDoc.Save(XmlFile);
                    }

                }

                objXmlDoc.Load(XmlFile);
            }

            
catch (System.Exception ex)
            
{
                
throw ex;
            }

            strXmlFile 
= XmlFile;
        }


        
/// <summary>
        
/// 根据xPath值,返回xPath下的所有下级子结节到一个DataView
        
/// </summary>
        
/// <param name="XmlPathNode">xPath值</param>
        
/// <returns>有数据则返回DataView,否则返回null</returns>

        public DataView GetData(string XmlPathNode)
        
{
            
//查找数据。返回一个DataView
            DataSet ds = new DataSet();
            
try
            
{
                StringReader read 
= new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
                ds.ReadXml(read);
                
return ds.Tables[0].DefaultView;
            }

            
catch
            
{
                
//throw;
                return null;
            }

        }


        
//...为了不至于把代码弄得太复杂,其它方法这里略去
    }


}


 回到正题,以下是访问system.webServer下的validation节点中的validateIntegratedModeConfiguration或myname的属性值的代码

当然用先using JIMMY.TOOLS 以下是主要代码

 

XmlControl xc = new XmlControl(Server.MapPath("~/web.config"), false"configuration");

DataView dv 
= xc.GetData("configuration/system.webServer/validation");

Response.Write(dv[
0][0].ToString() + "<br/>");//输出validateIntegratedModeConfiguration的属性值

Response.Write(dv[
0][1].ToString());//输出myname的属性值

甚至还可以将结果绑定到一个GridView上

GridView1.DataSource = dv;

GridView1.DataBind();

posted @ 2007-11-29 21:35  菩提树下的杨过  阅读(1143)  评论(0编辑  收藏  举报