XmlHelper操作Xml文件

根據(jù)Xml地址得到Xml內(nèi)容,可自定義緩存


XmlHelper使用教程:


        //用法
        /*
          XmlDocument xml = getXml(HttpContext.Current.Server.MapPath("~/qqq/sion.xml"), true);
                    XmlNode node = null;
                    if (type == "android")
                    {
                        node = xml.DocumentElement.SelectSingleNode("type[@name='oid']");
                    }
         * 
         *  HttpUtility.UrlPathEncode(node.Attributes["url"].Value)
         
         */
XmlHelper源碼



/// <summary>
/// 開發(fā)團隊:JsonsTeam
/// 官方主頁:http://hnxxbl.cn
/// </summary>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web;
using System.Web.Caching;

namespace JsonsTeamUtil.Helper
{
    public class XmlHelper
    {
        //用法
        /*
          XmlDocument xml = getXml(HttpContext.Current.Server.MapPath("~/qqq/sion.xml"), true);
                    XmlNode node = null;
                    if (type == "android")
                    {
                        node = xml.DocumentElement.SelectSingleNode("type[@name='oid']");
                    }
         * 
         *  HttpUtility.UrlPathEncode(node.Attributes["url"].Value)
         
         */
        /// <summary>
        /// 根據(jù)xml地址得到xml內(nèi)容
        /// </summary>
        /// <param name="path">絕對地址</param>
        /// <param name="cache">是否緩存(文件依賴)</param>
        /// <returns></returns>
        public static XmlDocument getXml(string path, bool cache)
        {
            XmlDocument xml;
            if (HttpRuntime.Cache[path] != null)
            {
                xml = HttpRuntime.Cache[path] as XmlDocument;
                return xml;
            }
            xml = new XmlDocument();
            xml.Load(path);
            if (cache)
            {
                CacheDependency depend = new CacheDependency(path);
                HttpRuntime.Cache.Insert(path, xml, depend);
            }
            return xml;
        }

    }
}


原文鏈接:XmlHelper操作Xml文件 根據(jù)Xml地址得到Xml內(nèi)容