Asp.net 操作XML实现网站配置

来源:互联网 发布:vim python ide 编辑:程序博客网 时间:2024/06/05 16:02

前端代码


 <div class="tab_con" style="display:block;">        <table class="form_table">            <col width="180px"><col>            <tbody>            <tr>                <th>站点名称:</th>                <td><asp:TextBox ID="webname" runat="server" CssClass="txtInput normal required" maxlength="100"></asp:TextBox><label>*</label></td>            </tr>            <tr>                <th>公司名称:</th>                <td><asp:TextBox ID="webcompany" runat="server" CssClass="txtInput normal required" maxlength="100"></asp:TextBox><label>*</label></td>            </tr>            <tr>                <th>网站域名:</th>                <td><asp:TextBox ID="weburl" runat="server" CssClass="txtInput normal required url" maxlength="250"></asp:TextBox><label>*以“http://”开头</label></td>            </tr>            <tr>                <th>联系电话:</th>                <td><asp:TextBox ID="webtel" runat="server" CssClass="txtInput normal" maxlength="50"></asp:TextBox></td>            </tr>            <tr>                <th>传真号码:</th>                <td><asp:TextBox ID="webfax" runat="server" CssClass="txtInput normal" maxlength="50"></asp:TextBox></td>            </tr>            <tr>                <th>管理员邮箱:</th>                <td><asp:TextBox ID="webmail" runat="server" CssClass="txtInput normal email" maxlength="100"></asp:TextBox></td>            </tr>            <tr>                <th>网站备案号:</th>                <td><asp:TextBox ID="webcrod" runat="server" CssClass="txtInput normal" maxlength="50"></asp:TextBox></td>            </tr>            <tr>                <th>首页标题(SEO):</th>                <td><asp:TextBox ID="webtitle" runat="server" CssClass="txtInput normal required" maxlength="250" style="width:350px;"></asp:TextBox><label>*自定义的首页标题</label></td>            </tr>            <tr>                <th>页面关健词(SEO):</th>                <td><asp:TextBox ID="webkeyword" runat="server" CssClass="txtInput" maxlength="250" style="width:350px;"></asp:TextBox>                    <label>页面关键词(keyword)</label></td>            </tr>            <tr>                <th>页面描述(SEO):</th>                <td><asp:TextBox ID="webdescription" runat="server" maxlength="250" TextMode="MultiLine" CssClass="small"></asp:TextBox>                    <label>页面描述(description)</label></td>            </tr>            <tr>                <th>网站版权信息:</th>                <td><asp:TextBox ID="webcopyright" runat="server" maxlength="500" TextMode="MultiLine" CssClass="small"></asp:TextBox><label>支持HTML格式</label></td>            </tr>            </tbody>        </table> <asp:button id="btnSubmit" runat="server" text="提交保存" cssclass="btnSubmit" onclick="btnSubmit_Click"></asp:button>    </div>

按钮事件

/// <summary>        /// 保存配置信息        /// </summary>        protected void btnSubmit_Click(object sender, EventArgs e)        {                       BLL.siteconfig bll = new BLL.siteconfig();            Model.siteconfig model = bll.loadConfig(Utils.GetXmlMapPath(DTKeys.FILE_SITE_XML_CONFING));            try            {                model.webname = webname.Text;                model.webcompany = webcompany.Text;                model.weburl = weburl.Text;                model.webtel = webtel.Text;                model.webfax = webfax.Text;                model.webmail = webmail.Text;                model.webcrod = webcrod.Text;                model.webtitle = webtitle.Text;                model.webkeyword = webkeyword.Text;                model.webdescription = Utils.DropHTML(webdescription.Text);                model.webcopyright = webcopyright.Text;                model.webpath = webpath.Text;                model.webmanagepath = webmanagepath.Text;                model.webstatus = int.Parse(webstatus.Text.Trim());                model.webclosereason = webclosereason.Text;                model.webcountcode = webcountcode.Text;                model.staticstatus = int.Parse(staticstatus.SelectedValue);                model.staticextension = staticextension.Text;                model.memberstatus = int.Parse(memberstatus.SelectedValue);                model.commentstatus = int.Parse(commentstatus.SelectedValue);                model.logstatus = int.Parse(logstatus.SelectedValue);                model.emailstmp = emailstmp.Text;                model.emailport = int.Parse(emailport.Text.Trim());                model.emailfrom = emailfrom.Text;                model.emailusername = emailusername.Text;                //判断密码是否更改                if (emailpassword.Text.Trim() != defaultpassword)                {                    model.emailpassword = DESEncrypt.Encrypt(emailpassword.Text, model.sysencryptstring);                }                model.emailnickname = emailnickname.Text;                model.attachpath = attachpath.Text;                model.attachextension = attachextension.Text;                model.attachsave = int.Parse(attachsave.SelectedValue);                model.attachfilesize = int.Parse(attachfilesize.Text.Trim());                model.attachimgsize = int.Parse(attachimgsize.Text.Trim());                model.attachimgmaxheight = int.Parse(attachimgmaxheight.Text.Trim());                model.attachimgmaxwidth = int.Parse(attachimgmaxwidth.Text.Trim());                model.thumbnailheight = int.Parse(thumbnailheight.Text.Trim());                model.thumbnailwidth = int.Parse(thumbnailwidth.Text.Trim());                model.watermarktype = int.Parse(watermarktype.SelectedValue);                model.watermarkposition = int.Parse(watermarkposition.Text.Trim());                model.watermarkimgquality = int.Parse(watermarkimgquality.Text.Trim());                model.watermarkpic = watermarkpic.Text;                model.watermarktransparency = int.Parse(watermarktransparency.Text.Trim());                model.watermarktext = watermarktext.Text;                model.watermarkfont = watermarkfont.Text;                model.watermarkfontsize = int.Parse(watermarkfontsize.Text.Trim());                bll.saveConifg(model, Utils.GetXmlMapPath(DTKeys.FILE_SITE_XML_CONFING));                JscriptMsg("修改系统信息成功啦!", "sys_config.aspx", "Success");            }            catch            {                JscriptMsg("文件写入失败,请检查是否有权限!", "", "Error");            }        }

BLL层


using System;using System.Collections.Generic;using System.Text;using System.IO;using System.Web;using System.Web.Caching;using DTcms.Common;namespace DTcms.BLL{    public partial class siteconfig    {        private readonly DAL.siteconfig dal = new DAL.siteconfig();        /// <summary>        ///  读取配置文件        /// </summary>        public Model.siteconfig loadConfig(string configFilePath)        {            Model.siteconfig model = CacheHelper.Get<Model.siteconfig>(DTKeys.CACHE_SITE_CONFIG);            if (model == null)            {                CacheHelper.Insert(DTKeys.CACHE_SITE_CONFIG, dal.loadConfig(configFilePath), configFilePath);                model = CacheHelper.Get<Model.siteconfig>(DTKeys.CACHE_SITE_CONFIG);            }            return model;        }        /// <summary>        /// 读取客户端站点配置信息        /// </summary>        public Model.siteconfig loadConfig(string configFilePath, bool isClient)        {            Model.siteconfig model = CacheHelper.Get<Model.siteconfig>(DTKeys.CACHE_SITE_CONFIG_CLIENT);            if (model == null)            {                model = dal.loadConfig(configFilePath);                model.templateskin = model.webpath + "templates/" + model.templateskin;                CacheHelper.Insert(DTKeys.CACHE_SITE_CONFIG_CLIENT, model, configFilePath);            }            return model;        }        /// <summary>        ///  保存配置文件        /// </summary>        public Model.siteconfig saveConifg(Model.siteconfig model, string configFilePath)        {            return dal.saveConifg(model, configFilePath);        }    }}

DAL层


using System;using System.Collections.Generic;using System.Text;using DTcms.Common;namespace DTcms.DAL{    /// <summary>    /// 数据访问类:站点配置    /// </summary>    public partial class siteconfig    {        private static object lockHelper = new object();        /// <summary>        ///  读取站点配置文件        /// </summary>        public Model.siteconfig loadConfig(string configFilePath)        {            return (Model.siteconfig)SerializationHelper.Load(typeof(Model.siteconfig), configFilePath);        }        /// <summary>        /// 写入站点配置文件        /// </summary>        public Model.siteconfig saveConifg(Model.siteconfig model, string configFilePath)        {            lock (lockHelper)            {                SerializationHelper.Save(model, configFilePath);            }            return model;        }    }}

MODEL类


using System;using System.Collections.Generic;using System.Text;namespace DTcms.Model{    /// <summary>    /// 站点配置实体类    /// </summary>    [Serializable]    public class siteconfig    {        public siteconfig()        { }        private string _webname = "";        private string _webcompany = "";        private string _weburl = "";        private string _webtel = "";        private string _webfax = "";        private string _webmail = "";        private string _webcrod = "";        private string _webtitle = "";        private string _webkeyword = "";        private string _webdescription = "";        private string _webcopyright = "";        private string _webpath = "";        private string _webmanagepath = "";        private int _webstatus = 1;        private string _webclosereason = "";        private string _webcountcode = "";        private int _staticstatus = 0;        private string _staticextension = "";        private int _memberstatus = 1;        private int _commentstatus = 0;        private int _logstatus = 0;        private string _emailstmp = "";        private int _emailport = 25;        private string _emailfrom = "";        private string _emailusername = "";        private string _emailpassword = "";        private string _emailnickname = "";        private string _attachpath = "";        private string _attachextension = "";        private int _attachsave = 1;        private int _attachfilesize = 0;        private int _attachimgsize = 0;        private int _attachimgmaxheight = 0;        private int _attachimgmaxwidth = 0;        private int _thumbnailheight = 0;        private int _thumbnailwidth = 0;        private int _watermarktype = 0;        private int _watermarkposition = 9;        private int _watermarkimgquality = 80;        private string _watermarkpic = "";        private int _watermarktransparency = 10;        private string _watermarktext = "";        private string _watermarkfont = "";        private int _watermarkfontsize = 12;        //===============当前模板配置信息===============        private string _templateskin = "default";        //==============系统安装时配置信息==============        private string _sysdatabaseprefix = "dt_";        private string _sysencryptstring = "DTcms";        /// <summary>        /// 站点名称        /// </summary>        public string webname        {            get { return _webname; }            set { _webname = value; }        }        /// <summary>        /// 公司名称        /// </summary>        public string webcompany        {            get { return _webcompany; }            set { _webcompany = value; }        }        /// <summary>        /// 网站域名        /// </summary>        public string weburl        {            get { return _weburl; }            set { _weburl = value; }        }        /// <summary>        /// 联系电话        /// </summary>        public string webtel        {            get { return _webtel; }            set { _webtel = value; }        }        /// <summary>        /// 传真号码        /// </summary>        public string webfax        {            get { return _webfax; }            set { _webfax = value; }        }        /// <summary>        /// 管理员邮箱        /// </summary>        public string webmail        {            get { return _webmail; }            set { _webmail = value; }        }        /// <summary>        /// 网站备案号        /// </summary>        public string webcrod        {            get { return _webcrod; }            set { _webcrod = value; }        }        /// <summary>        /// 网站首页标题        /// </summary>        public string webtitle        {            get { return _webtitle; }            set { _webtitle = value; }        }        /// <summary>        /// 页面关健词        /// </summary>        public string webkeyword        {            get { return _webkeyword; }            set { _webkeyword = value; }        }        /// <summary>        /// 页面描述        /// </summary>        public string webdescription        {            get { return _webdescription; }            set { _webdescription = value; }        }        /// <summary>        /// 网站版权信息        /// </summary>        public string webcopyright        {            get { return _webcopyright; }            set { _webcopyright = value; }        }        /// <summary>        /// 网站安装目录        /// </summary>        public string webpath        {            get { return _webpath; }            set { _webpath = value; }        }        /// <summary>        /// 网站管理目录        /// </summary>        public string webmanagepath        {            get { return _webmanagepath; }            set { _webmanagepath = value; }        }        /// <summary>        /// 是否关闭网站        /// </summary>        public int webstatus        {            get { return _webstatus; }            set { _webstatus = value; }        }        /// <summary>        /// 关闭原因描述        /// </summary>        public string webclosereason        {            get { return _webclosereason; }            set { _webclosereason = value; }        }        /// <summary>        /// 网站统计代码        /// </summary>        public string webcountcode        {            get { return _webcountcode; }            set { _webcountcode = value; }        }        /// <summary>        /// 是否开启静态        /// </summary>        public int staticstatus        {            get { return _staticstatus; }            set { _staticstatus = value; }        }        /// <summary>        /// 静态URL后缀        /// </summary>        public string staticextension        {            get { return _staticextension; }            set { _staticextension = value; }        }        /// <summary>        /// 开启会员功能        /// </summary>        public int memberstatus        {            get { return _memberstatus; }            set { _memberstatus = value; }        }        /// <summary>        /// 开启评论审核        /// </summary>        public int commentstatus        {            get { return _commentstatus; }            set { _commentstatus = value; }        }        /// <summary>        /// 后台管理日志        /// </summary>        public int logstatus        {            get { return _logstatus; }            set { _logstatus = value; }        }        /// <summary>        /// STMP服务器        /// </summary>        public string emailstmp        {            get { return _emailstmp; }            set { _emailstmp = value; }        }        /// <summary>        /// SMTP端口        /// </summary>        public int emailport        {            get { return _emailport; }            set { _emailport = value; }        }        /// <summary>        /// 发件人地址        /// </summary>        public string emailfrom        {            get { return _emailfrom; }            set { _emailfrom = value; }        }        /// <summary>        /// 邮箱账号        /// </summary>        public string emailusername        {            get { return _emailusername; }            set { _emailusername = value; }        }        /// <summary>        /// 邮箱密码        /// </summary>        public string emailpassword        {            get { return _emailpassword; }            set { _emailpassword = value; }        }        /// <summary>        /// 发件人昵称        /// </summary>        public string emailnickname        {            get { return _emailnickname; }            set { _emailnickname = value; }        }        /// <summary>        /// 附件上传目录        /// </summary>        public string attachpath        {            get { return _attachpath; }            set { _attachpath = value; }        }        /// <summary>        /// 附件上传类型        /// </summary>        public string attachextension        {            get { return _attachextension; }            set { _attachextension = value; }        }        /// <summary>        /// 附件保存方式        /// </summary>        public int attachsave        {            get { return _attachsave; }            set { _attachsave = value; }        }        /// <summary>        /// 文件上传大小        /// </summary>        public int attachfilesize        {            get { return _attachfilesize; }            set { _attachfilesize = value; }        }        /// <summary>        /// 图片上传大小        /// </summary>        public int attachimgsize        {            get { return _attachimgsize; }            set { _attachimgsize = value; }        }        /// <summary>        /// 图片最大高度(像素)        /// </summary>        public int attachimgmaxheight        {            get { return _attachimgmaxheight; }            set { _attachimgmaxheight = value; }        }        /// <summary>        /// 图片最大宽度(像素)        /// </summary>        public int attachimgmaxwidth        {            get { return _attachimgmaxwidth; }            set { _attachimgmaxwidth = value; }        }        /// <summary>        /// 生成缩略图高度(像素)        /// </summary>        public int thumbnailheight        {            get { return _thumbnailheight; }            set { _thumbnailheight = value; }        }        /// <summary>        /// 生成缩略图宽度(像素)        /// </summary>        public int thumbnailwidth        {            get { return _thumbnailwidth; }            set { _thumbnailwidth = value; }        }        /// <summary>        /// 图片水印类型        /// </summary>        public int watermarktype        {            get { return _watermarktype; }            set { _watermarktype = value; }        }        /// <summary>        /// 图片水印位置        /// </summary>        public int watermarkposition        {            get { return _watermarkposition; }            set { _watermarkposition = value; }        }        /// <summary>        /// 图片生成质量        /// </summary>        public int watermarkimgquality        {            get { return _watermarkimgquality; }            set { _watermarkimgquality = value; }        }        /// <summary>        /// 图片水印文件        /// </summary>        public string watermarkpic        {            get { return _watermarkpic; }            set { _watermarkpic = value; }        }        /// <summary>        /// 水印透明度        /// </summary>        public int watermarktransparency        {            get { return _watermarktransparency; }            set { _watermarktransparency = value; }        }        /// <summary>        /// 水印文字        /// </summary>        public string watermarktext        {            get { return _watermarktext; }            set { _watermarktext = value; }        }        /// <summary>        /// 文字字体        /// </summary>        public string watermarkfont        {            get { return _watermarkfont; }            set { _watermarkfont = value; }        }        /// <summary>        /// 文字大小(像素)        /// </summary>        public int watermarkfontsize        {            get { return _watermarkfontsize; }            set { _watermarkfontsize = value; }        }        /// <summary>        /// 当前模板        /// </summary>        public string templateskin        {            get { return _templateskin; }            set { _templateskin = value; }        }        /// <summary>        /// 数据库表前缀        /// </summary>        public string sysdatabaseprefix        {            get { return _sysdatabaseprefix; }            set { _sysdatabaseprefix = value; }        }        /// <summary>        /// 加密字符串        /// </summary>        public string sysencryptstring        {            get { return _sysencryptstring; }            set { _sysencryptstring = value; }        }    }}

缓存项的文件依赖


using System;using System.Collections.Generic;using System.Text;using System.Web;using System.Web.Caching;namespace DTcms.Common{    public class CacheHelper    {        /// <summary>        /// 创建缓存项的文件依赖        /// </summary>        /// <param name="key">缓存Key</param>        /// <param name="obj">object对象</param>        /// <param name="fileName">文件绝对路径</param>        public static void Insert(string key, object obj, string fileName)        {            //创建缓存依赖项            CacheDependency dep = new CacheDependency(fileName);            //创建缓存            HttpContext.Current.Cache.Insert(key, obj, dep);        }        /// <summary>        /// 创建缓存项过期        /// </summary>        /// <param name="key">缓存Key</param>        /// <param name="obj">object对象</param>        /// <param name="expires">过期时间(分钟)</param>        public static void Insert(string key, object obj, int expires)        {            HttpContext.Current.Cache.Insert(key, obj, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, expires, 0));        }        /// <summary>        /// 获取缓存对象        /// </summary>        /// <param name="key">缓存Key</param>        /// <returns>object对象</returns>        public static object Get(string key)        {            if (string.IsNullOrEmpty(key))            {                return null;            }            return HttpContext.Current.Cache.Get(key);        }        /// <summary>        /// 获取缓存对象        /// </summary>        /// <typeparam name="T">T对象</typeparam>        /// <param name="key">缓存Key</param>        /// <returns></returns>        public static T Get<T>(string key)        {            object obj = Get(key);            return obj == null ? default(T) : (T)obj;        }    }}

site.config

<?xml version="1.0"?><siteconfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">  <webname>DTcms内容管理系统</webname>  <webcompany>动力启航软件工作室</webcompany>  <weburl>http://demo.dtcms.net</weburl>  <webtel>13800138000</webtel>  <webfax>-</webfax>  <webmail>info@it134.cn</webmail>  <webcrod>粤ICP备11064298号</webcrod>  <webtitle>DTcms网站管理系统 - 动力启航_开源cms_NET开源_cms建站</webtitle>  <webkeyword>动力启航,DTCMS系统,ASP.NET开源,开源CMS,网站管理系统,互联网开发</webkeyword>  <webdescription>以ASP.NET(C#)+jQuery技术为中心,面向软件开发者、程序爱好者、网页设计师,服务于个人、企业的网站,同时也是一个专门结合AJAX技术开发友好界面、倍受欢迎用户体验的BS模式软件系统,打造自己的品牌。</webdescription>  <webcopyright>版权所有 动力启航 © Copyright 2009 - 2012. dtcms.net. All Rights Reserved.</webcopyright>  <webpath>/</webpath>  <webmanagepath>admin</webmanagepath>  <webstatus>1</webstatus>  <webclosereason>网站正在维护中,请稍候访问……</webclosereason>  <webcountcode><script src="http://s24.cnzz.com/stat.php?id=1996164&web_id=1996164&show=pic" language="JavaScript"></script></webcountcode>  <staticstatus>1</staticstatus>  <staticextension>html</staticextension>  <memberstatus>1</memberstatus>  <commentstatus>0</commentstatus>  <logstatus>1</logstatus>  <emailstmp>smtp.qq.com</emailstmp>  <emailport>25</emailport>  <emailfrom>info@it134.cn</emailfrom>  <emailusername>info@it134.cn</emailusername>  <emailpassword>EA4C94A47ED87546</emailpassword>  <emailnickname>动力启航</emailnickname>  <attachpath>upload</attachpath>  <attachextension>gif,jpg,png,bmp,rar,zip,doc,xls,txt</attachextension>  <attachsave>2</attachsave>  <attachfilesize>51200</attachfilesize>  <attachimgsize>10240</attachimgsize>  <attachimgmaxheight>800</attachimgmaxheight>  <attachimgmaxwidth>800</attachimgmaxwidth>  <thumbnailheight>300</thumbnailheight>  <thumbnailwidth>300</thumbnailwidth>  <watermarktype>2</watermarktype>  <watermarkposition>9</watermarkposition>  <watermarkimgquality>80</watermarkimgquality>  <watermarkpic>watermark.png</watermarkpic>  <watermarktransparency>5</watermarktransparency>  <watermarktext>动力启航</watermarktext>  <watermarkfont>Tahoma</watermarkfont>  <watermarkfontsize>12</watermarkfontsize>  <templateskin>green</templateskin>  <sysdatabaseprefix>dt_</sysdatabaseprefix>  <sysencryptstring>DTcms</sysencryptstring></siteconfig>

序列化

using System;using System.IO;using System.Xml.Serialization;namespace DTcms.Common{    public class SerializationHelper    {        public SerializationHelper() { }        /// <summary>        /// 反序列化        /// </summary>        /// <param name="type">对象类型</param>        /// <param name="filename">文件路径</param>        /// <returns></returns>        public static object Load(Type type, string filename)        {            FileStream fs = null;            try            {                // open the stream...                fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);                XmlSerializer serializer = new XmlSerializer(type);                return serializer.Deserialize(fs);            }            catch (Exception ex)            {                throw ex;            }            finally            {                if (fs != null)                    fs.Close();            }        }        /// <summary>        /// 序列化        /// </summary>        /// <param name="obj">对象</param>        /// <param name="filename">文件路径</param>        public static void Save(object obj, string filename)        {            FileStream fs = null;            // serialize it...            try            {                fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);                XmlSerializer serializer = new XmlSerializer(obj.GetType());                serializer.Serialize(fs, obj);            }            catch (Exception ex)            {                throw ex;            }            finally            {                if (fs != null)                    fs.Close();            }        }    }}

调用

<add key="Configpath" value="~/xmlconfig/site.config"/>

  //读取站点配置信息
            Model.siteconfig siteConfig = new BLL.siteconfig().loadConfig(DTKeys.FILE_SITE_XML_CONFING);

siteConfig.webpath 

原创粉丝点击