asp.net對XML檔案的一些簡單操作

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.