.NET开发笔记--对config文件的操作(2)

1、前台代码 sys_channel_edit.aspx

<tbody id="item_box">
                        <asp:Repeater ID="rptList" runat="server">
                        <ItemTemplate>
                        <tr class="td_c">
                            <td>
                                <input type="hidden" name="item_type" value="<%#Eval("type")%>" />
                                <input type="hidden" name="item_page" value="<%#Eval("page")%>" />
                                <span class="item_type"><%#GetPageTypeName(Eval("type").ToString())%></span>
                            </td>
                            <td><input type="hidden" name="old_item_name" value="<%#Eval("name")%>" /><input name="item_name" type="text" value="<%#Eval("name")%>" class="txtInput small" style="width:98%;" readonly="readonly" /></td>
                            <td><input name="item_path" type="text" value="<%#Eval("path")%>" class="txtInput small3" style="width:98%;" readonly="readonly" /></td>
                            <td><input name="item_pattern" type="text" value="<%#Eval("pattern")%>" class="txtInput small3" style="width:98%;" readonly="readonly" /></td>
                            <td><input name="item_querystring" type="text" value="<%#Eval("querystring")%>" class="txtInput small3" style="width:98%;" readonly="readonly" /></td>
                            <td><input name="item_templet" type="text" value="<%#Eval("templet")%>" class="txtInput small" style="width:98%;" readonly="readonly" /></td>
                            <td><img alt="编辑" src="../images/icon_edit.gif" class="operator" /><img alt="删除" src="../images/icon_del.gif" class="operator" /></td>
                        </tr>
                        </ItemTemplate>
                        </asp:Repeater>
                        </tbody>

2、后台获取rptList数据

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.sys_channel bll = new BLL.sys_channel();
            Model.sys_channel model = bll.GetModel(_id);
            txtName.Text = model.name;
            hidName.Value = model.name;
            txtTitle.Text = model.title;
            ddlModelId.SelectedValue = model.model_id.ToString();
            txtSortId.Text = model.sort_id.ToString();
            //绑定URL配置列表
            rptList.DataSource = new BLL.url_rewrite().GetList(model.id.ToString());
            rptList.DataBind();
        }
        #endregion

 #region 增加操作=================================
        private bool DoAdd()
        {
            Model.sys_channel model = new Model.sys_channel();
            BLL.sys_channel bll = new BLL.sys_channel();
            model.name = txtName.Text.Trim();
            model.title = txtTitle.Text.Trim();
            model.model_id = int.Parse(ddlModelId.SelectedValue);
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            int new_id = bll.Add(model); //保存
            if (new_id < 1)
            {
                return false;
            }
            #region 保存URL配置.开始===================================
            BLL.url_rewrite bll2 = new BLL.url_rewrite();
            bll2.Remove("channel", new_id.ToString()); //先删除
            string[] itemTypeArr = Request.Form.GetValues("item_type");
            string[] itemNameArr = Request.Form.GetValues("item_name");
            string[] itemPathArr = Request.Form.GetValues("item_path");
            string[] itemPatternArr = Request.Form.GetValues("item_pattern");
            string[] itemQuerystringArr = Request.Form.GetValues("item_querystring");
            string[] itemTempletArr = Request.Form.GetValues("item_templet");
            string[] itemPageArr = Request.Form.GetValues("item_page");
            if (itemTypeArr != null && itemNameArr != null && itemPathArr != null
                && itemPatternArr != null && itemQuerystringArr != null && itemTempletArr != null && itemPageArr != null)
            {
                if ((itemTypeArr.Length == itemNameArr.Length) && (itemNameArr.Length == itemPathArr.Length) && (itemPathArr.Length == itemPatternArr.Length)
                    && (itemPatternArr.Length == itemQuerystringArr.Length) && (itemQuerystringArr.Length == itemTempletArr.Length))
                {
                    for (int i = 0; i < itemTypeArr.Length; i++)
                    {
                        bll2.Add(new Model.url_rewrite
                        {
                            name = itemNameArr[i].Trim(),
                            path = itemPathArr[i].Trim(),
                            pattern = itemPatternArr[i].Trim(),
                            //page = GetPageName(model.name, itemTypeArr[i].Trim()), //源页面地址
                            page = itemPageArr[i].Trim(),
                            querystring = itemQuerystringArr[i].Trim(),
                            templet = itemTempletArr[i].Trim(),
                            channel = new_id.ToString(),
                            type = itemTypeArr[i].Trim(),
                            inherit = GetInherit(model.model_id, itemTypeArr[i].Trim()) //继承的类名
                        });
                    }
                }
            }
            #endregion 保存URL配置.结束

            return true;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            DTcms.BLL.sys_channel bll = new DTcms.BLL.sys_channel();
            DTcms.Model.sys_channel model = bll.GetModel(_id);
            model.name = txtName.Text.Trim();
            model.title = txtTitle.Text.Trim();
            model.model_id = int.Parse(ddlModelId.SelectedValue);
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            if (!bll.Update(model))
            {
                return false;
            }

            #region 保存URL配置.开始===================================
            BLL.url_rewrite bll2 = new BLL.url_rewrite();
            bll2.Remove("channel", model.id.ToString()); //先删除
            string[] itemTypeArr = Request.Form.GetValues("item_type");
            string[] itemNameArr = Request.Form.GetValues("item_name");
            string[] itemPathArr = Request.Form.GetValues("item_path");
            string[] itemPatternArr = Request.Form.GetValues("item_pattern");
            string[] itemQuerystringArr = Request.Form.GetValues("item_querystring");
            string[] itemTempletArr = Request.Form.GetValues("item_templet");
            string[] itemPageArr = Request.Form.GetValues("item_page");
            if (itemTypeArr != null && itemNameArr != null && itemPathArr != null
                && itemPatternArr != null && itemQuerystringArr != null && itemTempletArr != null)
            {
                if ((itemTypeArr.Length == itemNameArr.Length) && (itemNameArr.Length == itemPathArr.Length) && (itemPathArr.Length == itemPatternArr.Length)
                    && (itemPatternArr.Length == itemQuerystringArr.Length) && (itemQuerystringArr.Length == itemTempletArr.Length))
                {
                    for (int i = 0; i < itemTypeArr.Length; i++)
                    {
                        bll2.Add(new Model.url_rewrite
                        {
                            name = itemNameArr[i].Trim(),
                            path = itemPathArr[i].Trim(),
                            pattern = itemPatternArr[i].Trim(),
                            //page = GetPageName(model.name, itemTypeArr[i].Trim()), //源页面地址
                            page = itemPageArr[i].Trim(),
                            querystring = itemQuerystringArr[i].Trim(),
                            templet = itemTempletArr[i].Trim(),
                            channel = model.id.ToString(),
                            type = itemTypeArr[i].Trim(),
                            inherit = GetInherit(model.model_id, itemTypeArr[i].Trim()) //继承的类名
                        });
                    }
                }
            }
            #endregion 保存URL配置.结束

            return true;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("sys_channel", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误啦!", "", "Error");
                    return;
                }
                JscriptMsg("修改信息成功啦!", "sys_channel_list.aspx", "Success", "parent.loadChannelTree");
            }
            else //添加
            {
                ChkAdminLevel("sys_channel", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误啦!", "", "Error");
                    return;
                }
                JscriptMsg("添加信息成功啦!", "sys_channel_list.aspx", "Success", "parent.loadChannelTree");
            }
        }

    }

3、取得URL配置列表,对其的一些操作

/// <summary>
        /// 取得URL配制列表
        /// </summary>
        public List<Model.url_rewrite> GetList(string channel)
        {
            List<Model.url_rewrite> ls = new List<Model.url_rewrite>();
            string filePath = Utils.GetXmlMapPath(DTKeys.FILE_URL_XML_CONFING);
            XmlDocument doc = new XmlDocument();
            doc.Load(filePath);
            XmlNode xn = doc.SelectSingleNode("urls");
            foreach (XmlElement xe in xn.ChildNodes)
            {
                if (xe.NodeType != XmlNodeType.Comment && xe.Name.ToLower() == "rewrite")
                {
                    if (xe.Attributes["name"] != null)
                    {
                        if (!string.IsNullOrEmpty(channel))
                        {
                            if (channel.ToLower() == xe.Attributes["channel"].Value.ToLower())
                            {
                                Model.url_rewrite model = new Model.url_rewrite();
                                if (xe.Attributes["name"] != null)
                                    model.name = xe.Attributes["name"].Value;
                                if (xe.Attributes["path"] != null)
                                    model.path = xe.Attributes["path"].Value;
                                if (xe.Attributes["pattern"] != null)
                                    model.pattern = xe.Attributes["pattern"].Value;
                                if (xe.Attributes["page"] != null)
                                    model.page = xe.Attributes["page"].Value;
                                if (xe.Attributes["querystring"] != null)
                                    model.querystring = xe.Attributes["querystring"].Value;
                                if (xe.Attributes["templet"] != null)
                                    model.templet = xe.Attributes["templet"].Value;
                                if (xe.Attributes["channel"] != null)
                                    model.channel = xe.Attributes["channel"].Value;
                                if (xe.Attributes["type"] != null)
                                    model.type = xe.Attributes["type"].Value;
                                if (xe.Attributes["inherit"] != null)
                                    model.inherit = xe.Attributes["inherit"].Value;
                                ls.Add(model);
                            }
                        }
                        else
                        {
                            Model.url_rewrite model = new Model.url_rewrite();
                            if (xe.Attributes["name"] != null)
                                model.name = xe.Attributes["name"].Value;
                            if (xe.Attributes["path"] != null)
                                model.path = xe.Attributes["path"].Value;
                            if (xe.Attributes["pattern"] != null)
                                model.pattern = xe.Attributes["pattern"].Value;
                            if (xe.Attributes["page"] != null)
                                model.page = xe.Attributes["page"].Value;
                            if (xe.Attributes["querystring"] != null)
                                model.querystring = xe.Attributes["querystring"].Value;
                            if (xe.Attributes["templet"] != null)
                                model.templet = xe.Attributes["templet"].Value;
                            if (xe.Attributes["channel"] != null)
                                model.channel = xe.Attributes["channel"].Value;
                            if (xe.Attributes["type"] != null)
                                model.type = xe.Attributes["type"].Value;
                            if (xe.Attributes["inherit"] != null)
                                model.inherit = xe.Attributes["inherit"].Value;
                            ls.Add(model);
                        }

                    }
                }
            }
            return ls;
        }
        #endregion

/// <summary>
        /// 删除节点
        /// </summary>
        public bool Remove(string attrName, string attrValue)
        {
            string filePath = Utils.GetXmlMapPath(DTKeys.FILE_URL_XML_CONFING);
            XmlDocument doc = new XmlDocument();
            doc.Load(filePath);
            XmlNode xn = doc.SelectSingleNode("urls");
            XmlNodeList xnList = xn.ChildNodes;
            if (xnList.Count > 0)
            {
                for (int i = xnList.Count - 1; i >= 0; i--)
                {
                    XmlElement xe = (XmlElement)xnList.Item(i);
                    if (xe.Attributes[attrName].Value.ToLower() == attrValue.ToLower())
                    {
                        xn.RemoveChild(xe);
                    }
                }
                doc.Save(filePath);
                return true;
            }
            return false;
        }

/// <summary>
        /// 修改节点
        /// </summary>
        public bool Edit(Model.url_rewrite model)
        {
            string filePath = Utils.GetXmlMapPath(DTKeys.FILE_URL_XML_CONFING);
            XmlDocument doc = new XmlDocument();
            doc.Load(filePath);
            XmlNode xn = doc.SelectSingleNode("urls");
            XmlNodeList xnList = xn.ChildNodes;
            if (xnList.Count > 0)
            {
                foreach (XmlElement xe in xnList)
                {
                    if (xe.Attributes["name"].Value.ToLower() == model.name.ToLower())
                    {
                        if (!string.IsNullOrEmpty(model.path))
                            xe.SetAttribute("path", model.path);
                        else if (xe.Attributes["path"] != null)
                            xe.Attributes["path"].RemoveAll();
                        if (!string.IsNullOrEmpty(model.pattern))
                            xe.SetAttribute("pattern", model.pattern);
                        else if (xe.Attributes["pattern"] != null)
                            xe.Attributes["pattern"].RemoveAll();
                        if (!string.IsNullOrEmpty(model.page))
                            xe.SetAttribute("page", model.page);
                        else if (xe.Attributes["page"] != null)
                            xe.Attributes["page"].RemoveAll();
                        if (!string.IsNullOrEmpty(model.querystring))
                            xe.SetAttribute("querystring", model.querystring);
                        else if (xe.Attributes["querystring"] != null)
                            xe.Attributes["querystring"].RemoveAll();
                        if (!string.IsNullOrEmpty(model.templet))
                            xe.SetAttribute("templet", model.templet);
                        else if (xe.Attributes["templet"] != null)
                            xe.Attributes["templet"].RemoveAll();
                        if (!string.IsNullOrEmpty(model.channel))
                            xe.SetAttribute("channel", model.channel);
                        else if (xe.Attributes["channel"] != null)
                            xe.Attributes["channel"].RemoveAll();
                        if (!string.IsNullOrEmpty(model.type))
                            xe.SetAttribute("type", model.type);
                        else if (xe.Attributes["type"] != null)
                            xe.Attributes["type"].RemoveAll();
                        if (!string.IsNullOrEmpty(model.inherit))
                            xe.SetAttribute("inherit", model.inherit);
                        else if (xe.Attributes["inherit"] != null)
                            xe.Attributes["inherit"].RemoveAll();
                        doc.Save(filePath);
                        return true;
                    }
                }
            }
            return false;
        }

 /// <summary>
        /// 增加节点
        /// </summary>
        public bool Add(Model.url_rewrite model)
        {
            try
            {
                string filePath = Utils.GetXmlMapPath(DTKeys.FILE_URL_XML_CONFING);
                XmlDocument doc = new XmlDocument();
                doc.Load(filePath);
                XmlNode xn = doc.SelectSingleNode("urls");
                XmlElement xe = doc.CreateElement("rewrite");
                if (!string.IsNullOrEmpty(model.name))
                    xe.SetAttribute("name", model.name);
                if (!string.IsNullOrEmpty(model.path))
                    xe.SetAttribute("path", model.path);
                if (!string.IsNullOrEmpty(model.pattern))
                    xe.SetAttribute("pattern", model.pattern);
                if (!string.IsNullOrEmpty(model.page))
                    xe.SetAttribute("page", model.page);
                if (!string.IsNullOrEmpty(model.querystring))
                    xe.SetAttribute("querystring", model.querystring);
                if (!string.IsNullOrEmpty(model.templet))
                    xe.SetAttribute("templet", model.templet);
                if (!string.IsNullOrEmpty(model.channel))
                    xe.SetAttribute("channel", model.channel.ToString());
                if (!string.IsNullOrEmpty(model.type))
                    xe.SetAttribute("type", model.type);
                if (!string.IsNullOrEmpty(model.inherit))
                    xe.SetAttribute("inherit", model.inherit);
                xn.AppendChild(xe);
                doc.Save(filePath);
                return true;
            }
            catch
            {
                return false;
            }
        }

 

posted @ 2013-10-20 15:38  kanek  阅读(268)  评论(0编辑  收藏  举报