asp.net 2.0+xml留言板图
利用asp.net 2.0+xml实现无数据库留言板.其实就是xml为数据存储对象.利用asp.net来操作xml文档.
1.首先建立一个db的文件夹.并创建两个xml文档:admin.xml和db.xml
admin.xml
<?xml version="1.0" encoding="gb2312"?>
<administrators>
<admin>
<user>admin</user>
<password>admin888</password>
</admin>
</administrators>
db.xml
<?xml version="1.0" encoding="utf-8"?>
<root>
<node>
<id>100013</id>
<UserName>dsf</UserName>
<face>0.gif</face>
<SendTime>2007-3-23 15:31:13</SendTime>
<UserEmail>liqiang665@163.com</UserEmail>
<Content><![CDATA[<script language='javascript'>
while(1==1){
alert('111');
}
</script>]]></Content>
</node>
</root>
2.aspx.cs页如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Xml;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Gv_Bind();
if (Session["user"] != null)
{
if (Session["user"].ToString() == "Admin")
{
foreach (GridViewRow gridview in sa_gv_ly.Rows)
{
CheckBox chkbox;
Label lbl1;
chkbox = (CheckBox)gridview.FindControl("chkflag");
lbl1 = (Label)gridview.FindControl("sa_lbl_id");
chkbox.Visible = true;
lbl1.Visible = true;
}
sa_btn_login.Text = "修改";
sa_gv_ly.Columns[0].Visible = true;
select_all.Visible = true;
del_ly.Visible = true;
sa_lbl_username.Text = "管理员已登录";
}
}
}
}
public void Gv_Bind()
{
//创建文件注并打开xml数据库
FileStream stream = new FileStream(MapPath("db/db.xml"), FileMode.Open);
try
{
XmlDataDocument doc = new XmlDataDocument();
//把xml流读入dataset中
doc.DataSet.ReadXml(new StreamReader(stream));
if (doc.DataSet.Tables.Count == 0)
{
Response.Write("xml数据文件没有内容");
}
else
{
DataView dv = new DataView(doc.DataSet.Tables[0]);
dv.Sort = "SendTime desc";
PagedDataSource objpds = new PagedDataSource();
objpds.DataSource = dv; ;
objpds.AllowPaging = true;
objpds.PageSize = 10;
int currpage;
if (Request.QueryString["page"] != null)
currpage = Convert.ToInt32(Request.QueryString["page"]);
else
currpage = 1;
objpds.CurrentPageIndex = currpage - 1;
sa_first_page.Text = "首页(1)";
sa_prev_page.Text = "上一页";
sa_next_page.Text = "下一页";
sa_last_page.Text = "尾页("+objpds.PageCount+")";
if (!objpds.IsFirstPage)
{
sa_first_page.NavigateUrl = Request.CurrentExecutionFilePath + "?page=1";
sa_prev_page.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(currpage - 1);
}
if (!objpds.IsLastPage)
{
sa_next_page.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(currpage + 1);
sa_last_page.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" +
Convert.ToString(objpds.PageCount);
}
if (doc.DataSet.Tables.Count != 0 && doc.DataSet.Tables[0].Rows.Count != 0)
{
sa_gv_ly.DataSource = objpds;
sa_gv_ly.DataBind();
}
}
stream.Close();
}
catch (Exception ding)
{
Response.Write(ding.Message);
}
finally
{
stream.Close();
}
}
protected void sa_btn_submit_Click(object sender, EventArgs e)
{
int id;
string xmlfile = Server.MapPath("db/db.xml");
string strface =
(sa_hfd_face.Value.ToString().Substring(sa_hfd_face.Value.ToString().LastIndexOf("/")+1))!=""?(sa_hfd_face.Value.ToString().S
ubstring(sa_hfd_face.Value.ToString().LastIndexOf("/")+1)):"0.gif";
string username = Server.HtmlEncode(sa_tbx_input_username.Text != null ? sa_tbx_input_username.Text : "");
string useremail = Server.HtmlEncode(sa_tbx_input_email.Text != null ? sa_tbx_input_email.Text : "");
string usercontent = Server.HtmlEncode((sa_tbx_input_content.Text != null ? sa_tbx_input_content.Text :
"").Replace("\n\r", "|"));
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(xmlfile);
XmlNode xn1 = xmldoc.SelectSingleNode("root");
if (xn1.ChildNodes.Count ==0) id = 100000;
else id = Convert.ToInt32(xn1.LastChild.ChildNodes[0].InnerText) + 1;
string strxml = "<node>";
strxml += "<id>" + id + "</id>";
strxml += "<UserName><![CDATA[" + username + "]]></UserName>";
strxml += "<face>" + strface + "</face>";
strxml += "<SendTime>" + System.DateTime.Now.ToString() + "</SendTime>";
strxml += "<UserEmail>" + useremail + "</UserEmail>";
strxml += "<Content><![CDATA[" + usercontent + "]]></Content>";
strxml += "</node>";
XmlDocumentFragment xmlfrag = xmldoc.CreateDocumentFragment();
xmlfrag.InnerXml = strxml;
xmldoc.DocumentElement.InsertAfter(xmlfrag, xmldoc.DocumentElement.LastChild);
xmldoc.Save(xmlfile);
Gv_Bind();
Response.Redirect("Default.aspx");
}
protected void sa_btn_login_Click(object sender, EventArgs e)
{
string username = sa_tbx_username.Text != null ? sa_tbx_username.Text : "";
string userpassword = sa_tbx_userpassword.Text != null ? sa_tbx_userpassword.Text : "";
string xmlfile = Server.MapPath("db/admin.xml");
if (sa_btn_login.Text == "登录")
{
try
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(xmlfile);
XmlNode xnl = xmldoc.SelectSingleNode("administrators/admin");
if ((xnl.ChildNodes[0].InnerText == username) && (xnl.ChildNodes[1].InnerText == userpassword))
{
Session["user"] = "Admin";
Response.Write("<script language='javascript'>alert('登录成功');location='Default.aspx'</script>");
}
else
{
Response.Write("<script language='javascript'>alert('用户名或密码不正确
');location='Default.aspx'</script>");
}
Gv_Bind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
else
{
//保存修改的用户名和密码
sa_btn_login.Attributes.Add("OnClick", "return confirm('确定修改吗?')");
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(xmlfile);
XmlNode xn1 = xmldoc.SelectSingleNode("administrators/admin");
xn1.ChildNodes[0].InnerText = username;
xn1.ChildNodes[1].InnerText = userpassword;
xmldoc.Save(xmlfile);
Session.RemoveAll();
Response.Redirect("Default.aspx");
}
}
protected void sa_btn_out_Click(object sender, EventArgs e)
{
Session.RemoveAll();
Response.Redirect("Default.aspx");
}
protected void select_all_Click(object sender, EventArgs e)
{
CheckBox chkbox;
if (select_all.Text == "全选")
{
foreach (GridViewRow gridview in sa_gv_ly.Rows)
{
chkbox = (CheckBox)gridview.FindControl("chkflag");
chkbox.Checked = true;
}
select_all.Text = "取消";
}
else
{
foreach (GridViewRow gridview in sa_gv_ly.Rows)
{
chkbox = (CheckBox)gridview.FindControl("chkflag");
chkbox.Checked = false;
}
select_all.Text = "全选";
}
}
protected void del_ly_Click(object sender, EventArgs e)
{
string xmlfile = Server.MapPath("db/db.xml");
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(xmlfile);
XmlNode root = xmldoc.SelectSingleNode("root");
XmlNodeList xnl = xmldoc.SelectSingleNode("root").ChildNodes;
CheckBox chkbox;
int id;
if (select_all.Text == "全选")
{
//删除所选
foreach (GridViewRow gridview in sa_gv_ly.Rows)
{
id = Convert.ToInt32(((HiddenField)gridview.FindControl("sa_hfd_id")).Value);
chkbox = (CheckBox)gridview.FindControl("chkflag");
if (chkbox.Checked == true)
{
foreach (XmlNode xnsub1 in xnl)
{
XmlElement xe1 = (XmlElement)xnsub1;
if (xe1.ChildNodes[0].InnerText == id.ToString())
{
root.RemoveChild(xe1);
}
}
}
}
}
else
{
//删除所有
root.RemoveAll();
}
xmldoc.Save(xmlfile);
Response.Redirect("Default.aspx");
}
}

浙公网安备 33010602011771号