當前位置:
首頁 >
ASP.net Xml: ASP.net操作Xml
發布時間:2025/3/21
15
豆豆
生活随笔
收集整理的這篇文章主要介紹了
ASP.net Xml: ASP.net操作Xml
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?專題圖編號:ylbtechASPnetXml100010010
?
?
| XML課件PPT【在線PPT課件倡導者-ylb】 |
?
?http://wenku.baidu.com/view/bfac3ebe1a37f111f1855bc2.html
?
| 1,功能描述 |
? 這是一個基于.net操作Xml的案例示例,對Vote.xml文檔的CRUD(增讀改刪)操作。本配有PPT課件供大家參考學習。
?
| 2,技術與環境 |
?
| 操作系統: | windows | 開發語言: | C# |
| 開發框架: | ? | 數據庫: | 無 |
| 開發軟件: | Microsoft Visual Studio 2010 | ||
| 開發技術: | ?ASP.net+Xml | ||
| 課程總策劃: | yuanbo | 成員: | null |
| 個人主頁: | http://www.cnblogs.com/ylbtech/ | ||
| 科研團隊: | ylbtech | 教研團隊: | ylbtech |
?
| 3,/Vote.xml |
?
| 4,/App_Code/VoteInfo.cs |
?
| 5,/DemoXml.aspx.cs | ?ylb_tip: 這兒是.net對Xml操作的核心代碼區,請認真看,一定要把PPT課件看完,對根節點和節點要理解透 |
?
using System;using System.Xml; public partial class DemoXML : System.Web.UI.Page {/// <summary>/// ylb:1, 遍歷xml文檔/// </summary>private void BianLi(){ //提取xml文檔XmlDocument xd = new XmlDocument();xd.Load(Server.MapPath("Vote.xml"));//獲取根節點XmlNode root = xd.DocumentElement;//獲取節點列表XmlNodeList items = root.ChildNodes;//遍歷item項Response.Write("<pre>");foreach (XmlNode item in items){ //輸出屬性Response.Write(item.Attributes["belong"].Name+"="+item.Attributes["belong"].InnerText+"\t");//輸出子節點foreach (XmlNode p in item){Response.Write(p.Name+"="+p.InnerText+"\t");}Response.Write("\n");}Response.Write("</pre>");}/// <summary>/// ylb:2, 添加/// </summary>/// <param name="item"></param>private void Add(VoteInfo item){ //提取xml文檔XmlDocument xd = new XmlDocument();xd.Load(Server.MapPath("Vote.xml"));//獲取根節點XmlNode root = xd.DocumentElement;//創建元素XmlElement newItem = xd.CreateElement("item");XmlElement newID = xd.CreateElement("id");XmlElement newName = xd.CreateElement("name");XmlElement newNumber = xd.CreateElement("number");//配參newItem.SetAttribute("belong", item.Belong); //設置屬性 newID.InnerText = item.Id; //設置內容newName.InnerText = item.Name;newNumber.InnerText = item.Number;//裝配,實現其組織結構 root.AppendChild(newItem);newItem.AppendChild(newID);newItem.AppendChild(newName);newItem.AppendChild(newNumber);//保存xml文檔xd.Save(Server.MapPath("Vote.xml"));}/// <summary>/// ylb:3, 修改一項/// </summary>/// <param name="vote"></param>private void Update(VoteInfo vote){//提取xml文檔XmlDocument xd = new XmlDocument();xd.Load(Server.MapPath("Vote.xml"));//獲取根節點XmlNode root = xd.DocumentElement;//獲取節點類表XmlNodeList items = root.ChildNodes;//循環節點foreach (XmlNode item in items){//再循環節點foreach (XmlNode p in item){if (p.Name == "id" && p.InnerText == vote.Id){//則修改這一項//重設belong的值item.Attributes["belong"].InnerText = vote.Belong;//((XmlElement)item).SetAttribute("belong", vote.Belong);//給該節點(id)下的節點賦值p.NextSibling.InnerText = vote.Name;p.NextSibling.NextSibling.InnerText = vote.Number;}}}//保存xml文檔xd.Save(Server.MapPath("Vote.xml"));}/// <summary>/// ylb:4, 刪除一項/// </summary>/// <param name="id"></param>private void Delete(string id){ //提取xml文檔XmlDocument xd = new XmlDocument();xd.Load(Server.MapPath("vote.xml"));//獲取根節點XmlNode root = xd.DocumentElement;//獲取節點列表XmlNodeList items = root.ChildNodes;//循環節點foreach (XmlNode item in items){foreach (XmlNode p in item){if (p.Name == "id" && p.InnerText == id){ //移除該項 root.RemoveChild(item); }}}//保存xml文檔xd.Save(Server.MapPath("Vote.xml"));}protected void Page_Load(object sender, EventArgs e){//調用展示//ylb: 2,VoteInfo item = new VoteInfo("袁博自傳", "4", "ylb", "100");//Add(item);//ylb: 3, 根據id=3,修改 belong="天涯" name="莫離",number=110VoteInfo item2 = new VoteInfo("天涯", "3", "莫離", "110");//Update(item2);//ylb: 4, 刪除id=4的項Delete("3");//ylb: 1, 遍歷Xml文檔//BianLi(); } }?
| 6,示例|講解案例下載 |
博客園講解:
?????? http://ylbtech.cnblogs.com/
百度文庫開發文檔:
?????? http://passport.baidu.com/?business&aid=6&un=ylbtech#7
谷歌開源代碼下載:
????? ?http://code.google.com/p/ylbtechaspnet/downloads/list
請單擊“ylbtechXml100010010”
?
| 作者:ylbtech 出處:http://ylbtech.cnblogs.com/ 本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。 |
?
總結
以上是生活随笔為你收集整理的ASP.net Xml: ASP.net操作Xml的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 点连通度的求法
- 下一篇: linux下bochs启动黑屏解决办法