在c#web开发中很对时候都用到了编辑xml文件。现贴出源码,供大家学习参考。
1.xml文件。
<?xml version="1.0" encoding="utf-8"?>
<smallfoolsRoot>
<poems>
<author>王维</author>
<title>竹里馆</title>
<content>独坐幽篁里,弹琴复长啸。深林人不知,明月来相照。</content>
</poems>
<poems>
<author>孟浩然</author>
<title>宿建德江</title>
<content>移舟泊烟渚,日暮客愁新。野旷天低树,江清月近人</content>
</poems>
<poems>
<author>李白</author>
<title>杜陵绝句</title>
<content>南登杜陵上,北望五陵间。秋水明落日,流光灭远山</content>
</poems>
<poems>
<author>李白杰</author>
<title>望庐山瀑布</title>
<content>日照香炉生紫烟,遥看瀑布挂前川。飞流直下三千尺,疑是银河落九天。</content>
</poems>
<poems>
<author>李商隐</author>
<title>锦瑟</title>
<content>锦瑟无端五十弦,一弦一柱思华年。庄生晓梦迷蝴蝶,望帝春心托杜鹃。</content>
</poems>
<poems>
<author>李杰波</author>
<title>凉风</title>
<content>沧海月明珠有泪,蓝田日暖玉生烟。此情可待成追忆,只是当时已惘然。</content>
</poems>
</smallfoolsRoot>
2.ListXml.aspx
A.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>分页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<tr>
<td>
<%# Eval("Id") %>
</td>
<td>
<a href="GetOneXml.aspx?Xid=<%# Eval("Id")%>">
<%# Eval("author")%>
</a>
</td>
<td>
<%# Eval("title")%>
</td>
<td>
<%# Eval("content")%>
</td>
<td>
<a href="AddXml.aspx">添加</a>
</td>
<td>
<a href="EditXml.aspx?Xid=<%# Eval("Id")%>">修改</a>
</td>
<td>
<a href="DelXml.aspx?Xid=<%# Eval("Id")%>">删除</a>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<asp:Panel runat="server" ID="pagepanel" Width="501px">
<asp:Label runat="server" ID="countlabel"></asp:Label>
<asp:Label runat="server" ID="currentlabel"></asp:Label>
<asp:HyperLink runat="server" ID="firstlinkbutton" Text="首页"></asp:HyperLink>
<asp:HyperLink runat="server" ID="prevlinkbutton" Text="上一页"></asp:HyperLink>
<asp:HyperLink runat="server" ID="newlinkbutton" Text="下一页"></asp:HyperLink>
<asp:HyperLink runat="server" ID="lastlinkbutton" Text="尾页"></asp:HyperLink>
转到<asp:TextBox ID="TextBox1" runat="server" Width="19px"></asp:TextBox>页
<asp:Button ID="Button1" runat="server" Text="Go" OnClick="Button1_Click" />
</asp:Panel>
<asp:HiddenField ID="HiddenField1" runat="server" />
</div>
</form>
</body>
</html>
后台代码:
public partial class ListXml : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.PagerBind();
}
}
private void PagerBind()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("XMLFile/XMLFile.xml"));
if (ds.Tables.Count > 0)
{
this.Repeater1.Visible = true;
this.pagepanel.Visible = true;
PagedDataSource objPds = new PagedDataSource();
objPds.DataSource = ds.Tables[0].DefaultView;
objPds.AllowPaging = true;
objPds.PageSize = 3;
this.HiddenField1.Value = objPds.PageCount.ToString();
this.countlabel.Text = "共:" + objPds.PageCount.ToString() + "页";
int CurPage;
if (Request.QueryString["Page"] != null)
CurPage = Convert.ToInt32(Request.QueryString["Page"]);
else
CurPage = 1;
objPds.CurrentPageIndex = CurPage - 1;
this.currentlabel.Text = "当前页:" + CurPage.ToString();
this.TextBox1.Text = CurPage.ToString();
if (!objPds.IsFirstPage)
{
this.firstlinkbutton.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
this.prevlinkbutton.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
}
if (!objPds.IsLastPage)
{
this.newlinkbutton.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
this.lastlinkbutton.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + objPds.PageCount.ToString();
}
this.Repeater1.DataSource = objPds;
this.Repeater1.DataBind();
}
else
{
this.Repeater1.Visible = false;
this.pagepanel.Visible = false;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int CurPage;
int i = Convert.ToInt32(this.HiddenField1.Value);
if (Int32.TryParse(this.TextBox1.Text, out CurPage))
{
if (CurPage < 1 || CurPage > i)
{
Response.Redirect(Request.CurrentExecutionFilePath + "?page=1");
}
else
{
Response.Redirect(Request.CurrentExecutionFilePath + "?page=" + CurPage);
}
}
}
}
增删改查关键代码
---查询
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["Xid"] != null)
{
//操作二:获得单个节点的值
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("XMLFile/XMLFile.xml"));
//XmlNodeList xmlNodeList = xmlDoc.SelectNodes("smallfoolsRoot/poems[contains(author,'" + Request.QueryString["Xid"].ToString() + "')]");//糊模查找记录
XmlNodeList xmlNodeList = xmlDoc.SelectNodes("smallfoolsRoot/poems[id='" + Request.QueryString["Xid"].ToString() + "']");//查找
XmlNode xmlNode = xmlNodeList.Item(0);
this.tbauthor.Text = xmlNode["author"].InnerText;
this.tbtitle.Text = xmlNode["title"].InnerText;
this.tbcontent.Text = xmlNode["content"].InnerText;
}
}
---修改
public partial class EditXml : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["Xid"] != null)
{
//操作二:获得单个节点的值
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("XMLFile/XMLFile.xml"));
XmlNodeList xmlNodeList = xmlDoc.SelectNodes("smallfoolsRoot/poems[id='" + Request.QueryString["Xid"].ToString() + "']");//查找
XmlNode xmlNode = xmlNodeList.Item(0);
this.tbauthor.Text = xmlNode["author"].InnerText;
this.tbtitle.Text = xmlNode["title"].InnerText;
this.tbcontent.Text = xmlNode["content"].InnerText;
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Request.QueryString["Xid"] != null)
{
// 操作六:编辑某个节点
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("XMLFile/XMLFile.xml"));
//获得节点列表
XmlNodeList xmlNodeList = xmlDoc.SelectNodes("smallfoolsRoot/poems[id='" + Request.QueryString["Xid"].ToString() + "']");//查找
XmlNode xmlNode = xmlNodeList.Item(0);
xmlNode["author"].InnerText = this.tbauthor.Text;
xmlNode["title"].InnerText = this.tbtitle.Text;
xmlNode["content"].InnerText = this.tbcontent.Text;
xmlDoc.Save(Server.MapPath("XMLFile/XMLFile.xml"));
Response.Redirect("Default.aspx");
}
}
}
--添加
protected void Button1_Click(object sender, EventArgs e)
{
//操作四:添加一个节点
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("XMLFile/XMLFile.xml"));
//创建一个新节点
XmlElement newElement = xmlDoc.CreateElement("poems");
//创建newElement下的节点
XmlElement elid = xmlDoc.CreateElement("id");
XmlElement elauthor = xmlDoc.CreateElement("author");
XmlElement eltitle = xmlDoc.CreateElement("title");
XmlElement elcontent = xmlDoc.CreateElement("content");
elid.InnerText = ReturnCount();
elauthor.InnerText = this.tbaddauthor.Text.Trim();
eltitle.InnerText = this.tbaddtitle.Text.Trim();
elcontent.InnerText = this.tbaddcontent.Text.Trim();
//将newElement下的节点加到newElement上
newElement.AppendChild(elid);
newElement.AppendChild(elauthor);
newElement.AppendChild(eltitle);
newElement.AppendChild(elcontent);
//将newElement加入到xml文件中(加在最后一条记录上)
xmlDoc.DocumentElement.AppendChild(newElement);
//如果要插到某条记录之后也可以用(加在第一条记录之后)
//xmlDoc.DocumentElement.InsertAfter(newElement,xmlDoc.DocumentElement.ChildNodes.Item(0));
//如果要插到某条记录之前也可以用(加在第一条记录之前)
//xmlDoc.DocumentElement.InsertBefore(newElement,xmlDoc.DocumentElement.ChildNodes.Item(0));
//存盘
xmlDoc.Save(Server.MapPath("XMLFile/XMLFile.xml"));
Response.Redirect("Default.aspx");
}
public string ReturnCount()
{
//返回最后的ID值并+1
string i = string.Empty;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("XMLFile/XMLFile.xml"));
XmlNode xmlNode = xmlDoc.DocumentElement.LastChild;
if (xmlNode != null)
{
i = Convert.ToString(Convert.ToUInt32(xmlNode["id"].InnerText) + 1);
}
return i;
}
}
--删除
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["Xid"] != null)
{
//操作五:删除某个节点
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("XMLFile/XMLFile.xml"));
XmlNodeList xmlNodeList = xmlDoc.SelectNodes("smallfoolsRoot/poems[id='" + Request.QueryString["Xid"].ToString() + "']");//查找
XmlNode xmlNode = xmlNodeList.Item(0);
xmlNode.ParentNode.RemoveChild(xmlNode);
xmlDoc.Save(Server.MapPath("XMLFile/XMLFile.xml"));
Response.Redirect("Default.aspx");
}
}
}