在開始之前,先建立一個smallfools.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>
</smallfoolsRoot>
下面的操作都在這個xml檔案裡進行。
操作一:讀取整個XML檔案,並在DataGrid裡顯示出來:
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("smallfools.xml"));
if (ds.Tables.Count>0)
{
this.DataGrid1.DataSource = ds.Tables[0].DefaultView;
this.DataGrid1.DataBind();
}
操作二:獲得第一個節點的值
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("smallfools.xml"));
XmlNode xmlNode = xmlDoc.DocumentElement.FirstChild;
if (xmlNode!=null)
{
this.tbauthor.Text = xmlNode["author"].InnerText;
this.tbtitle.Text = xmlNode["title"].InnerText;
this.tbcontent.Text = xmlNode["content"].InnerText;
ViewState["Count"] = 0;
}
操作三:查看某一個節點的內容
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("smallfools.xml"));
XmlNodeList xmlNodeList = xmlDoc.DocumentElement.ChildNodes;
XmlNode xmlNode = xmlNodeList.Item(0);
this.tbauthor.Text = xmlNode["author"].InnerText;
this.tbtitle.Text = xmlNode["title"].InnerText;
this.tbcontent.Text = xmlNode["content"].InnerText;
操作四:添加一個節點
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("smallfools.xml"));
//建立一個新節點
XmlElement newElement = xmlDoc.CreateElement("poems");
//建立newElement下的節點
XmlElement elauthor = xmlDoc.CreateElement("author");
XmlElement eltitle = xmlDoc.CreateElement("title");
XmlElement elcontent = xmlDoc.CreateElement("content");
elauthor.InnerText = this.tbaddauthor.Text.Trim();
eltitle.InnerText = this.tbaddtitle.Text.Trim();
elcontent.InnerText = this.tbaddcontent.Text.Trim();
//將newElement下的節點加到newElement上
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("smallfools.xml"));
操作五:刪除某個節點
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("smallfools.xml"));
XmlNode xmlNode = xmlDoc.DocumentElement.ChildNodes.Item(0);
xmlNode.ParentNode.RemoveChild(xmlNode);
xmlDoc.Save(Server.MapPath("smallfools.xml"));
操作六:編輯某個節點
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("smallfools.xml"));
//獲得節點列表
XmlNode xmlNode = xmlDoc.DocumentElement.ChildNodes.Item(1);
xmlNode["author"].InnerText = this.tbauthor.Text;
xmlNode["title"].InnerText = this.tbtitle.Text;
xmlNode["content"].InnerText = this.tbcontent.Text;
xmlDoc.Save(Server.MapPath("smallfools.xml"));
操作七:尋找記錄
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("smallfools.xml"));
XmlNodeList nodelist = xmlDoc.SelectNodes("smallfoolsRoot/poems[author='"+this.tbsearch.Text.Trim()+"']");
操作八:糊模尋找記錄
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("smallfools.xml"));
XmlNodeList nodelist = xmlDoc.SelectNodes("smallfoolsRoot/poems[contains(author,'"+this.tbsearch.Text.Trim()+"')]");