XML增删查改
<?xml version="1.0" encoding="utf-8"?>
<testRoot>
<poems>
<name>王维</name>
<title>竹里馆</title>
<content>独坐幽篁里,弹琴复长啸。深林人不知,明月来相照。</content>
</poems>
<poems>
<name>孟浩然</name>
<title>宿建德江</title>
<content>移舟泊烟渚,日暮客愁新。野旷天低树,江清月近人</content>
</poems>
<poems>
<name>李白</name>
<title>杜陵绝句</title>
<content>南登杜陵上,北望五陵间。秋水明落日,流光灭远山</content>
</poems>
<poems>
<name>李白</name>
<title>望庐山瀑布</title>
<content>日照香炉生紫烟,遥看瀑布挂前川。飞流直下三千尺,疑是银河落九天。</content>
</poems>
<poems>
<name>李商隐</name>
<title>锦瑟</title>
<content>锦瑟无端五十弦,一弦一柱思华年。庄生晓梦迷蝴蝶,望帝春心托杜鹃。沧海月明珠有泪,蓝田日暖玉生烟。此情可待成追忆,只是当时已惘然。</content>
</poems>
<poems>
<name>名称一1</name>
<title>标题一</title>
<content>内容一</content>
</poems>
<poems>
<name>名称一2</name>
<title>标题一</title>
<content>内容一</content>
</poems>
<poems>
<name>编辑名称一</name>
<title>编辑标题一</title>
<content>编辑内容一</content>
</poems>
</testRoot>
=================================================================================================================================================================================
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Xml" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//SelectXml1();
//SelectXml2();
//addXml3();
//DeleteXml();
//EditXml();
//searchXml();
//searchLikeXml();
//UpdateXml();
deleteNode();
SelectXml();
}
}
/// <summary>
/// 查询XML
/// </summary>
private void SelectXml()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("/test.xml"));
if (ds.Tables.Count > 0)
{
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
}
/// <summary>
/// 获得第一个节点的值
/// </summary>
private void SelectXml1()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("/test.xml"));
XmlNode xmlNode = xmlDoc.DocumentElement.FirstChild;
if (xmlNode != null)
{
Response.Write(xmlNode["name"].InnerText + ",");
}
}
/// <summary>
/// 查看某一个节点的内容
/// </summary>
private void SelectXml2()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("/test.xml"));
XmlNodeList xmlNodeList = xmlDoc.DocumentElement.ChildNodes;
XmlNode xmlNode = xmlNodeList.Item(1);
Response.Write(xmlNode["name"].InnerText + ",");
}
/// <summary>
/// 添加一个节点
/// </summary>
private void addXml()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("test.xml"));
//创建一个新节点
XmlElement newElement = xmlDoc.CreateElement("poems");
//创建newElement下的节点
XmlElement elauthor = xmlDoc.CreateElement("name");
XmlElement eltitle = xmlDoc.CreateElement("title");
XmlElement elcontent = xmlDoc.CreateElement("content");
elauthor.InnerText = "名称一";
eltitle.InnerText = "标题一";
elcontent.InnerText = "内容一";
//将newElement下的节点加到newElement上
newElement.AppendChild(elauthor);
newElement.AppendChild(eltitle);
newElement.AppendChild(elcontent);
xmlDoc.DocumentElement.AppendChild(newElement);//将newElement加入到xml文件中(加在最后一条记录上)
//xmlDoc.DocumentElement.InsertAfter(newElement,xmlDoc.DocumentElement.ChildNodes.Item(0));//如果要插到某条记录之后也可以用(加在第一条记录之后)
//xmlDoc.DocumentElement.InsertBefore(newElement,xmlDoc.DocumentElement.ChildNodes.Item(0)); //如果要插到某条记录之前也可以用(加在第一条记录之前)
xmlDoc.Save(Server.MapPath("/test.xml"));//存盘
}
/// <summary>
/// 删除某个节点
/// </summary>
private void DeleteXml()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("/test.xml"));
XmlNode xmlNode = xmlDoc.DocumentElement.ChildNodes.Item(7);//要删除的节点索引
xmlNode.ParentNode.RemoveChild(xmlNode);
xmlDoc.Save(Server.MapPath("/test.xml"));
}
/// <summary>
///
/// </summary>
private void EditXml()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("/test.xml"));
//获得节点列表
XmlNode xmlNode = xmlDoc.DocumentElement.ChildNodes.Item(7);
xmlNode["name"].InnerText = "编辑名称一";
xmlNode["title"].InnerText = "编辑标题一";
xmlNode["content"].InnerText = "编辑内容一";
xmlDoc.Save(Server.MapPath("/test.xml"));
}
/// <summary>
/// 编辑某个节点
/// </summary>
private void UpdateXml()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("/test.xml"));
XmlNodeList nodeList = xmlDoc.SelectSingleNode("testRoot").ChildNodes;//获取bookstore节点的所有子节点
foreach (XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
if (xe.GetAttribute("name") == "风云")//如果name属性值为“风云”
{
xe.SetAttribute("name", "发明");
//如果下面有子节点在下走
XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach (XmlNode xn1 in nls)//遍历
{
XmlElement xe2 = (XmlElement)xn1;//转换类型
if (xe2.Name == "pass")//如果找到
{
xe2.InnerText = "66666";//则修改
break;
}
}
break;
}
}
xmlDoc.Save(Server.MapPath("/test.xml"));//保存
}
//删出节点
private void deleteNode()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("/test.xml"));
XmlNodeList nodeList = xmlDoc.SelectSingleNode("testRoot").ChildNodes;//获取bookstore节点的所有子节点
foreach (XmlNode xn in nodeList)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("name") == "编辑名称一")
{
//xe.RemoveAttribute("name");//删除name属性
xe.RemoveAll();//删除该节点的全部内容
break;
}
}
xmlDoc.Save(Server.MapPath("/test.xml"));//保存
}
/// <summary>
/// 查找某个节点 直等
/// </summary>
private void searchXml()
{
string str = "李白";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("/test.xml"));
XmlNodeList nodelist = xmlDoc.SelectNodes("testRoot/poems[name='" + str + "']");
Response.Write(nodelist.Count);
}
/// <summary>
/// 查找某个节点 糊模查找记录 LIKE
/// </summary>
private void searchLikeXml()
{
string str = "编辑";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("/test.xml"));
XmlNodeList nodelist = xmlDoc.SelectNodes("testRoot/poems[contains(name,'" + str + "')]");
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>XML操作(增删查改)</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</form>
</body>
</html>