用c#实现简单的xml操作代码

2009年3月7日星期六

用c#实现简单的xml操作代码

新建个xml文件叫bookstore.xml

代码如下:

view plaincopy to clipboardprint?



c#入门经典
更新Karli Watson
98.00





c#入门经典
更新Karli Watson
98.00



在页面中拖四个button控件:

代码如下:
view plaincopy to clipboardprint?
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>





操作xml













<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>





操作xml















在后台代码中如下:

view plaincopy to clipboardprint?
using System;
using System.Data;
using System.Configuration;
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.Xml;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void upxml_btn_Click(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("bookstore.xml"));
XmlNodeList nodelist = xmldoc.SelectSingleNode("bookstore").ChildNodes;
foreach(XmlNode x in nodelist)
{
XmlElement xe = (XmlElement)x;
if (xe.GetAttribute("genre") == "fantasy")
{
xe.SetAttribute("genre", "更新fantasy");
XmlNodeList nls = xe.ChildNodes;
foreach (XmlNode xls in nls)
{
XmlElement xlse = (XmlElement)xls;
if (xlse.Name == "author")
{
xlse.InnerText = "更新Karli Watson";
break;
}

}
break;
}
}
xmldoc.Save(Server.MapPath("bookstore.xml"));
this.returnmessage.Text = "修改成功";

}
protected void Addxml_btn_Click(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("bookstore.xml"));
XmlNode root = xmldoc.SelectSingleNode("bookstore");
XmlElement xe = xmldoc.CreateElement("book");
xe.SetAttribute("genre","小林");
xe.SetAttribute("IBSN","1-2-3-4");
XmlElement xesub1 = xmldoc.CreateElement("title");
xesub1.InnerText = "c#教程"; //设置文本节点
xe.AppendChild(xesub1);
XmlElement xesub2 = xmldoc.CreateElement("author");
xesub2.InnerText = "小一";
xe.AppendChild(xesub2);
XmlElement xesub3 = xmldoc.CreateElement("price");
xesub3.InnerText = "58.03";
xe.AppendChild(xesub3);
root.AppendChild(xe);
xmldoc.Save(Server.MapPath("bookstore.xml"));
this.returnmessage.Text = "添加成功";

}
protected void delxml_btn_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("bookstore.xml"));
XmlNodeList xnl = xmlDoc.SelectSingleNode("bookstore").ChildNodes;
foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("genre") == "fantasy")
{
xe.RemoveAttribute("genre");
}
else if (xe.GetAttribute("genre") == "更新fantasy")
{
xe.RemoveAll();
}

}
xmlDoc.Save(Server.MapPath("bookstore.xml"));
this.returnmessage.Text = "删除成功";

}
protected void showxml_btn_Click(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("bookstore.xml"));
XmlNode xn = xmldoc.SelectSingleNode("bookstore");
XmlNodeList xe = xn.ChildNodes;
foreach(XmlNode x in xe)
{
XmlElement xe1 = (XmlElement)x;
this.returnmessage.Text = xe1.GetAttribute("genre");


}

}
}
using System;
using System.Data;
using System.Configuration;
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.Xml;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void upxml_btn_Click(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("bookstore.xml"));
XmlNodeList nodelist = xmldoc.SelectSingleNode("bookstore").ChildNodes;
foreach(XmlNode x in nodelist)
{
XmlElement xe = (XmlElement)x;
if (xe.GetAttribute("genre") == "fantasy")
{
xe.SetAttribute("genre", "更新fantasy");
XmlNodeList nls = xe.ChildNodes;
foreach (XmlNode xls in nls)
{
XmlElement xlse = (XmlElement)xls;
if (xlse.Name == "author")
{
xlse.InnerText = "更新Karli Watson";
break;
}

}
break;
}
}
xmldoc.Save(Server.MapPath("bookstore.xml"));
this.returnmessage.Text = "修改成功";

}
protected void Addxml_btn_Click(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("bookstore.xml"));
XmlNode root = xmldoc.SelectSingleNode("bookstore");
XmlElement xe = xmldoc.CreateElement("book");
xe.SetAttribute("genre","小林");
xe.SetAttribute("IBSN","1-2-3-4");
XmlElement xesub1 = xmldoc.CreateElement("title");
xesub1.InnerText = "c#教程"; //设置文本节点
xe.AppendChild(xesub1);
XmlElement xesub2 = xmldoc.CreateElement("author");
xesub2.InnerText = "小一";
xe.AppendChild(xesub2);
XmlElement xesub3 = xmldoc.CreateElement("price");
xesub3.InnerText = "58.03";
xe.AppendChild(xesub3);
root.AppendChild(xe);
xmldoc.Save(Server.MapPath("bookstore.xml"));
this.returnmessage.Text = "添加成功";

}
protected void delxml_btn_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("bookstore.xml"));
XmlNodeList xnl = xmlDoc.SelectSingleNode("bookstore").ChildNodes;
foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("genre") == "fantasy")
{
xe.RemoveAttribute("genre");
}
else if (xe.GetAttribute("genre") == "更新fantasy")
{
xe.RemoveAll();
}

}
xmlDoc.Save(Server.MapPath("bookstore.xml"));
this.returnmessage.Text = "删除成功";

}
protected void showxml_btn_Click(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("bookstore.xml"));
XmlNode xn = xmldoc.SelectSingleNode("bookstore");
XmlNodeList xe = xn.ChildNodes;
foreach(XmlNode x in xe)
{
XmlElement xe1 = (XmlElement)x;
this.returnmessage.Text = xe1.GetAttribute("genre");


}

}
}


一个简单的显示,修改,删除xml代码写好了,写的有点简单,明天要去上班了,终于结束了长时期没工作的痛苦阶段。


0 评论:

发表评论