.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 ...
随机推荐
- thinkphp+Jquery MiniUI 开发 管理系统
Jquery Miniui JS 兼容IE6+的浏览器,非常适合在企业内部使用. 管理系统等都非常方便. 在企业内部正在使用的是前期asp.net +fineui实现的管理系统.经过了几年的实践检验. ...
- 算法笔记_070:BellmanFord算法简单介绍(Java)
目录 1 问题描述 2 解决方案 2.1 具体编码 1 问题描述 何为BellmanFord算法? BellmanFord算法功能:给定一个加权连通图,选取一个顶点,称为起点,求取起点到其它所有顶 ...
- kvm 虚拟化 SMP(对称多处理器)介绍及配置
一.SMP(对称多处理器)介绍 1)SMP简介 在计算机技术很普及和日益发达的今天,以 Intel.IBM 为代表的一些大公司推动着中央处理器(CPU)技术的飞速发展和更新换代.在如今计算机系统中,多 ...
- C++ 代码风格准则:POD
作者:一根筋的傻瓜链接:https://www.zhihu.com/question/36379130/answer/69853366来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...
- CVTE电话面试
Cvte电话面试 1. SVM和逻辑回归的相同不同点 2. 特征值和奇异值的区别 3. 如何找到全局最优解,梯度下降和牛顿法区别 4. 防止过拟合的方法 5. 随机森林和ADBOOST方差和偏置 6. ...
- easy ui datagrid 数据分页
参照easyui官方网站提供的demo写了个datagrid数据分页的demo, 具体参数我就不一一罗列了,详细见官方网站, 这里只介绍一下具体的注意事项和常乃用到的几项, $('#test').da ...
- 阿里云Ubuntu部署java web - 文件夹
文件夹(点击章节标题阅读): 阿里云Ubuntu部署java web(1) - 系统配置 ssh链接server(使用终端远程链接) 加入用户 给用户赋予运 ...
- Web App、Hybrid App、Native APP对比
- 180508 - 解决有关VIVO的2018-04-01安全补丁导致的APP闪退问题
解决有关VIVO的2018-04-01安全补丁导致的APP闪退问题 [√]问题原因猜测4: 最终解决方案 [√]问题原因猜测3: 尝试解决 [√成功] [×]问题原因猜测2: 尝试解决 [×失败] [ ...
- 如何启动mininet实例上的wireshark图形界面
启动wireshark 要启动mininet实例上的wireshark的图形界面,其实关键点只有两个: 保证宿主机上安装了X11 使用ssh -Y mininet@192.168.56.102 登录进 ...