專題圖編號: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 |
<?xml version="1.0" encoding="utf-8"?> <vote> <item belong="三國演義"> <id>1</id> <name>曉梅</name> <number>60</number> </item> <item belong="西游記"> <id>2</id> <name>小駱</name> <number>34</number> </item> <item belong="天涯"> <id>3</id> <name>莫離</name> <number>110</number> </item> </vote>
4,/App_Code/VoteInfo.cs |
using System; /// <summary> ///Vote 的摘要說明 /// </summary> public class VoteInfo { // 1, Attributes /// <summary> /// 圖書名稱 /// </summary> string _belong; /// <summary> /// 編號 /// </summary> string _id; /// <summary> /// 作者 /// </summary> string _name; /// <summary> /// 書本數量 /// </summary> string _number; // 2, Struct public VoteInfo(string belong, string id,string name, string number) { this._belong = belong; this._id = id; this._name = name; this._number = number; } public VoteInfo() { } //封裝字段 public string Belong { get { return _belong; } set { _belong = value; } } public string Id { get { return _id; } set { _id = value; } } public string Name { get { return _name; } set { _name = value; } } public string Number { get { return _number; } set { _number = value; } } }
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=110 VoteInfo item2 = new VoteInfo("天涯", "3", "莫離", "110"); //Update(item2); //ylb: 4, 刪除id=4的項 Delete("3"); //ylb: 1, 遍歷Xml文檔 //BianLi(); } }
6,示例|講解案例下載 |
博客園講解:
百度文庫開發文檔:
http://passport.baidu.com/?business&aid=6&un=ylbtech#7
谷歌開源代碼下載:
http://code.google.com/p/ylbtechaspnet/downloads/list
請單擊“ylbtechXml100010010”
![]() |
作者:ylbtech 出處:http://ylbtech.cnblogs.com/ 本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。 |