asp.net对XML文件的一些简单操作

在开始之前,先建立一个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()+"')]");

posted on 2007-11-26 19:28  Bēniaǒ  阅读(380)  评论(0编辑  收藏  举报