.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 < )
{
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 = ; 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 = ; 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 > )
{
for (int i = xnList.Count - ; i >= ; 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 > )
{
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;
}
}
.NET开发笔记--对config文件的操作(2)的更多相关文章
- .NET开发笔记--对config文件的操作(1)
1先写一些常用的公共类: 在Web.config文件中的配置: <!-- appSettings网站信息配置--> <appSettings> <add key=&quo ...
- .NET开发笔记--对config文件的操作(3)
1.添加新节点前进行判断看是否已存在相同的属性值,若存在进行更新,不存在则进行添加操作. protected bool AddPizza() { //初始化id int newId; string f ...
- Python入门笔记(16):对文件的操作(2)
一.文件系统 从系统角度来看,文件系统是对文件存储器空间进行组织和分配,负责文件存储并对存入的文件进行保护和检索的系统.具体地说,它负责为用户建立文件,存入.读出.修改.转储文件,控制文件的存取,当用 ...
- loadrunner 脚本开发-调用java jar文件远程操作Oracle数据库测试
调用java jar文件远程操作Oracle数据库测试 by:授客 QQ:1033553122 测试环境 数据库:linux 下Oracle_11g_R2 Loadrunner:11 备注:想学ora ...
- Beego 学习笔记12:文件的操作
文件的操作 1> 此事例操作的是text文件 2> 文件的操作有读取text内容,将内容写入到文件中,删除文件,创建文件 3> 新建一个控制器,名为rwfil ...
- 《UNIX-Shell编程24学时教程》读书笔记Chap3,4 文件,目录操作
Chap3 文件操作 P28 在这章中,要着重记住一些常用的选项,要有使用正则表达式的思维,能更快达到目的.----@im天行 3.1 列文件名 .profile sh的初始化脚本: .kshr ...
- Python入门笔记(15):对文件的操作(1)
一.文件对象 我理解的文件对象就是一个接口,通过这个接口对文件进行相关操作. <Python 核心编程>上说的很晦涩,这里没有深刻理解到,希望有人能解释给我听. >>> ...
- Appcan开发笔记:导出Excel文件
Appcan IDE为4.0+; appcan提供了导出文件的方法 appcan.file.write 文件会自动创建,要解决的事情是Excel用字符串输出,可以考虑 csv(逗号间隔).HTML.X ...
- [开发笔记]-C#判断文件类型
判断文件真实的类型,不是通过扩展名来判断: /// <summary> /// 判断文件格式 /// http://www.cnblogs.com/babycool /// </su ...
随机推荐
- 无废话MVC入门教程一[概述、环境安装、创建项目]
(转载) 本文目标 1.对MVC有初步的了解 2.能够在VS2010的基础之上安装MVC3的开发和运行环境 3.对MVC框架有概括性的认识 本文目录 1.什么是MVC 2.VS2010安装MVC3 3 ...
- VUE 数组更新
1.数据方法分类: (1)原数组改变 push pop unshift shift reverse sort splice (2)原数组未变,生成新数组 slice concat filter map ...
- 嵌入web字体
@font-face模块 可以帮助我们轻松解决Web页面中使用优雅字体的方式,而且我们可以根据需要自定义多种字体,但是每个@font-face只能定义一种,若需要多个字体就启用多个@f ...
- 我的Go语言学习之旅二:入门初体验 Hello World
好吧,全部的程序猿们都已经习惯了.学习不论什么一门语言,我们都会以Hello World实例開始我们的学习,我也不例外.先来一个简单的样例 打开编辑器 (能够用记事本,我已经习惯 Notepad++了 ...
- 02-spring学习-配置bean
在spring的IOC容器中配置Bean 一,在xml中通过bean节点来配置bean: class:bean的类名,通过反射的方式在IOC容器中创建Bean,所以要求bean中必须有无参的构造器 i ...
- mongodb安装的两条命令
1. 安装 下载并安装,注意安装方式为custom,路径自定义(d:\chengxu\mongodb),安装成功后在mongodb文件夹下新建data文件夹(内新建db文件夹)和logs文件夹(内新建 ...
- Android开发笔记之:Handler Runnable与Thread的区别详解
在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口:Thread类是在java.lang包中定义的.一 个类只要继承了Thread类同时覆写了本类中的run( ...
- web前端--移动端适配总结
转自:https://segmentfault.com/a/1190000011586301 作者:Devinnn meta标签到底做了什么事情 做过移动端适配的小伙伴一定有遇到过这行代码: < ...
- 【ZYNQ-7000开发之九】使用VDMA在PL和PS之间传输视频流数据
[ZYNQ-7000开发之九]使用VDMA在PL和PS之间传输视频流数据 原创 2016年01月14日 11:35:02 标签: VDMA / zynq / zedbaord / AXI 10384 ...
- [Java Web]Hibernate基础总结(四)
性能优化 在大数据量遍历时(比如查找消息敏感词),须要手动使用clear方法释放缓存中的数据,防止缓存中数据过多浪费内存. 1+N问题:将Fetch设为LAZY能够在须要时才发出sql语句,或者设置B ...