简单的读取xml文件

1.xml文件

<?xml version="1.0" encoding="utf-8" ?>
<web>
  <website key="0" value="title"/>
  <website key="1" value="name"/>
  <website key="2" value="url"/>
  <website key="3" value="createDate"/>
  <website key="4" value="desc"/>
  <testAA key="fds" value="123" />
  <testAA key="HH" value="123" />
  <testAA key="RRE" value="123" />
  <net>
    <aspx key="c#" value="linq" />
    <aspx key="f#" value="fn" />
  </net>
</web>

2.读取xml文件后台代码

  /// <summary>  
        /// 检查某个文件是否存在  
        /// </summary>  
        /// <param name="filePath">文件的物理路径</param>  
        /// <returns></returns>  
        private bool IsExistFiles(string filePath)
        {
            try
            {
                if (System.IO.File.Exists(filePath))
                    return true;
            }
            catch (Exception ex) { throw new Exception(ex.Message + "\r\n" + "May be other error:文件不存在或禁止访问!"); }
            return false;
        }  
        protected void btnGetXml_Click(object sender, EventArgs e)
        {
            string content = "Content";
            Dictionary<string, string> dic = ReadConfig("CreateHtml.config", "web/website");
            if (dic == null)
                return;
            foreach (KeyValuePair<string, string> kv in dic)
            {
                content += "<Br />" + kv.Key + ":" + kv.Value;  
            }
            Response.Write(content);
        }
        ///<summary>  
        /// 读取配置文件某节点的个数  
        ///</summary>  
        ///<param name="path">配置文件的路径</param>  
        ///<param name="nodeName">要获取的节点</param>  
        private Dictionary<string, string> ReadConfig(string path, string nodeName)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            string absoPath = string.Empty;  //绝对路径  
            try
            {
                absoPath = System.Web.HttpContext.Current.Server.MapPath(path);
                if (IsExistFiles(absoPath))
                {
                    XmlDocument xd = new XmlDocument();
                    xd.Load(absoPath);
                    XmlNodeList nodeList = xd.SelectNodes(nodeName);  //得到相应节点的集合  
                    if (nodeList != null && nodeList.Count > 0)
                        for (int i = 0; i < nodeList.Count; i++)
                            dic.Add(nodeList.Item(i).Attributes["key"].Value, nodeList.Item(i).Attributes["value"].Value);
                }
                return dic;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        } 
3.前台页面显示 语句

<%#Eval("content") %>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值