xml檔案格式
<?xml version="1.0" encoding="utf-8"?><br /><messageList><br /> <message id="1"><br /> <Name>熊貓</Name><br /> <QQ>123456</QQ><br /> <Email>panda@qq.com</Email><br /> <Content>asfdsafasdfasdfsad我是內容</Content><br /> <Time>2009-10-1</Time><br /> </message><br /></messageList>
增加>>>>
XmlDocument xmld = new XmlDocument();<br /> xmld.Load(Server.MapPath("App_Code/Message.xml"));//載入xml檔案<br /> XmlNode root = xmld.SelectSingleNode("messageList");//尋找messageList節點</p><p> int i = xmld.SelectSingleNode("messageList").ChildNodes.Count + 1;<br /> XmlElement xml0 = xmld.CreateElement("message");<br /> xml0.SetAttribute("id", i.ToString());</p><p> XmlElement xml1 = xmld.CreateElement("Name");//建立一個Name節點<br /> xml1.InnerText = this.txtName.Value.Trim();<br /> xml0.AppendChild(xml1);//添加</p><p> XmlElement xml2 = xmld.CreateElement("QQ");//建立QQ節點<br /> xml2.InnerText = this.txtQQ.Value.Trim();<br /> xml0.AppendChild(xml2);//添加</p><p> XmlElement xml3 = xmld.CreateElement("Email");//建立一個Email節點<br /> xml3.InnerText = this.txtEmail.Value.Trim();<br /> xml0.AppendChild(xml3);//添加</p><p> XmlElement xml4 = xmld.CreateElement("Content");//建立一個Content節點<br /> xml4.InnerText = this.txtContent.Value;<br /> xml0.AppendChild(xml4);//添加</p><p> XmlElement xml5 = xmld.CreateElement("Time");//建立一個Time節點<br /> xml5.InnerText = DateTime.Now.ToString("yyyy-MM-dd");<br /> xml0.AppendChild(xml5);//添加</p><p> root.AppendChild(xml0);</p><p> xmld.Save(Server.MapPath("App_Code/Message.xml"));
刪除>>>>
XmlDocument xmld = new XmlDocument();</p><p> xmld.Load(Server.MapPath("App_Code/Message.xml"));</p><p> XmlNodeList list = xmld.SelectSingleNode("messageList").ChildNodes;</p><p> foreach (XmlNode var in list)<br /> {<br /> XmlElement xmle = (XmlElement)var;</p><p> if (xmle.GetAttribute("id") == Id)//如果ID等於文本的值就刪除<br /> {<br /> xmle.RemoveAll();<br /> xmld.Save(Server.MapPath("App_Code/Message.xml"));//刪除後記得一定要儲存<br /> Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('刪除成功!!');", true);<br /> }<br /> else<br /> {<br /> Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('刪除失敗!!');", true);<br /> }<br /> }
更改>>>>
XmlDocument xmld = new XmlDocument();<br /> xmld.Load(Server.MapPath("App_Code/Message.xml"));</p><p> XmlNodeList list = xmld.SelectSingleNode("messageList").ChildNodes;</p><p> foreach (XmlNode xmln in list)<br /> {<br /> XmlElement xmle = (XmlElement)xmln;//格式轉換<br /> if (Request["ID"].Equals(xmle.GetAttribute("id")))<br /> {<br /> xmle.ChildNodes[0].InnerText = this.txtName.Value;<br /> xmle.ChildNodes[1].InnerText = this.txtQQ.Value;<br /> xmle.ChildNodes[2].InnerText = this.txtEmail.Value;<br /> xmle.ChildNodes[3].InnerText = this.txtContent.Value;</p><p> xmld.Save(Server.MapPath("App_Code/Message.xml"));<br /> Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('修改成功!!!');location.href='MessageList.aspx'",true);<br /> }<br /> }
查詢>>>>>
//下面的代碼開始是用頁面編碼的方式實現的<br />XmlDocument xmld = new XmlDocument();//構建xml文檔<br /> xmld.Load(Server.MapPath("App_Code/Message.xml"));//載入</p><p> XmlNodeList list = xmld.SelectSingleNode("messageList").ChildNodes;//擷取子節點<br /> if (list!=null)<br /> {<br /> foreach (XmlNode node in list)<br /> {<br /> XmlElement xmle = (XmlElement)node;//轉換<br />//這裡到時候明天補上