.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 ...
随机推荐
- 对PHP中类、继承、抽象的理解(个人总结)
1,PHP中的类可以被继承:A extends B. 2,类被声明为abstract时:a.该类就仅仅包含模版,我们就不能创建该类的实例对象,必须通过继承来使用new创建对象. b.继承一个抽象类的时 ...
- RESTful最佳实践
哲学 不要为了RESTful而RESTful 在能表达清楚的情况下,简单就是美 接口路径设计 接口设计原则 URI指向的是唯一的资源对象 示例: 指向ID为yanbo.ai的Account对象 GET ...
- vb.net小试三层架构
在对三层架构有了初步了解后,用vb.net做了一个小的程序,真的很小,仅仅是为了体现一下三层之间机制.下面是我设计的操作界面: 还有程序集和类的分布情况, 接下来是数据的设计,数据库用到的是SQL S ...
- Game Center Achievements and Leaderboards part 1 <转>
原地址:http://blog.sina.com.cn/s/blog_6b3661a901013xb0.html This is the first part of a step-by-step tu ...
- Zoho CEO:云计算泡沫巨大 Salesforce仅仅是新的Siebel
最近Zoho CEO - Sridhar Vembu接受科技博客媒体Diginomica的专訪,从独特的眼光和见解.讲述了云计算行业环境.SaaS公司的生存状态.商业观念以及Zoho的商业模式. Sr ...
- hibernate 多对一关联
(转自尚学堂教学内容) 注解多对一: package com.bjsxt.hibernate; import javax.persistence.Entity; import javax.pers ...
- 利用POI进行Excel的导出
需求:将用户的违约金信息导出为excel表格格式 步骤 1. 数据库中增加按钮的值(注意上级编号要和页面隐藏域中的相等) DZ内容(页面加载时根据SJBH查询数据库内容,读取DZ字段信息并加载样式及方 ...
- spring jdbc框架
spring+jdbc 1.jdbc编程的特点: 模板编程 固定代码+动态的参数 spring产生dataSource JdbcTemplate{//模板编程 private DataSource d ...
- ajax 异步 通信 小例子 servlet与 jsp异步 post方法
post请求 url后面加参数 接收不到的,必须 放到send("use"=user)形式 还要加上 xhr.setRequestHeader("Content-Type ...
- RabbitMQ的应用场景以及基本原理介绍(转载)
1.背景 RabbitMQ是一个由erlang开发的AMQP(Advanved Message Queue)的开源实现. 2.应用场景 2.1异步处理 场景说明:用户注册后,需要发注册邮件和注册短信, ...