日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#操作xml文件實例詳解

C#操作xml文件實例是如何的呢?讓我們先看看問題:

成都創(chuàng)新互聯(lián)長期為上千客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為天山企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計、網(wǎng)站制作,天山網(wǎng)站改版等技術(shù)服務(wù)。擁有十余年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

已知有一個XML文件(bookstore.xml)如下:

 
 
 
  1. ﹤?xml version="1.0" encoding="gb2312"?﹥
  2. ﹤bookstore﹥
  3. ﹤book genre="fantasy" ISBN="2-3631-4"﹥
  4. ﹤title﹥Oberon's Legacy﹤/title﹥
  5. ﹤author﹥Corets, Eva﹤/author﹥
  6. ﹤price﹥5.95﹤/price﹥
  7. ﹤/book﹥
  8. ﹤/bookstore﹥

C#操作xml文件實例1、

往﹤bookstore﹥節(jié)點中插入一個﹤book﹥節(jié)點:

 
 
 
  1. XmlDocument xmlDoc=new XmlDocument();
  2. xmlDoc.Load("bookstore.xml");
  3. XmlNode root=xmlDoc.SelectSingleNode("bookstore");//查找﹤bookstore﹥
  4. XmlElement xe1=xmlDoc.CreateElement("book");//創(chuàng)建一個﹤book﹥節(jié)點
  5. xe1.SetAttribute("genre","李贊紅");//設(shè)置該節(jié)點genre屬性
  6. xe1.SetAttribute("ISBN","2-3631-4");//設(shè)置該節(jié)點ISBN屬性
  7. XmlElement xesub1=xmlDoc.CreateElement("title");
  8. xesub1.InnerText="CS從入門到精通";//設(shè)置文本節(jié)點
  9. xe1.AppendChild(xesub1);//添加到﹤book﹥節(jié)點中
  10. XmlElement xesub2=xmlDoc.CreateElement("author");
  11. xesub2.InnerText="候捷";
  12. xe1.AppendChild(xesub2);
  13. XmlElement xesub3=xmlDoc.CreateElement("price");
  14. xesub3.InnerText="58.3";
  15. xe1.AppendChild(xesub3);
  16. root.AppendChild(xe1);//添加到﹤bookstore﹥節(jié)點中
  17. xmlDoc.Save("bookstore.xml");
  18. //================

C#操作xml文件實例結(jié)果為:

 
 
 
  1. ﹤?xml version="1.0" encoding="gb2312"?﹥
  2. ﹤bookstore﹥
  3. ﹤book genre="fantasy" ISBN="2-3631-4"﹥
  4. ﹤title﹥Oberon's Legacy﹤/title﹥
  5. ﹤author﹥Corets, Eva﹤/author﹥
  6. ﹤price﹥5.95﹤/price﹥
  7. ﹤/book﹥
  8. ﹤book genre="李贊紅" ISBN="2-3631-4"﹥
  9. ﹤title﹥CS從入門到精通﹤/title﹥
  10. ﹤author﹥候捷﹤/author﹥
  11. ﹤price﹥58.3﹤/price﹥
  12. ﹤/book﹥
  13. ﹤/bookstore﹥

C#操作xml文件實例2、

修改節(jié)點:將genre屬性值為“李贊紅“的節(jié)點的genre值改為“update李贊紅”,將該節(jié)點的子節(jié)點﹤author﹥的文本修改為“亞勝”。

 
 
 
  1. XmlNodeList nodeList=xmlDoc.
  2. SelectSingleNode("bookstore").ChildNodes;
  3. //獲取bookstore節(jié)點的所有子節(jié)點
  4. foreach(XmlNode xn in nodeList)
  5. //遍歷所有子節(jié)點
  6. {
  7. XmlElement xe=(XmlElement)xn;
  8. //將子節(jié)點類型轉(zhuǎn)換為XmlElement類型
  9. if(xe.GetAttribute("genre")=="李贊紅")
  10. //如果genre屬性值為“李贊紅”
  11. {
  12. xe.SetAttribute("genre","update李贊紅");
  13. //則修改該屬性為“update李贊紅”
  14. XmlNodeList nls=xe.ChildNodes;
  15. //繼續(xù)獲取xe子節(jié)點的所有子節(jié)點
  16. foreach(XmlNode xn1 in nls)//遍歷
  17. {
  18. XmlElement xe2=(XmlElement)xn1;
  19. //轉(zhuǎn)換類型
  20. if(xe2.Name=="author")//如果找到
  21. {
  22. xe2.InnerText="亞勝";//則修改
  23. break;//找到退出來就可以了
  24. }
  25. }
  26. break;
  27. }
  28. }
  29. xmlDoc.Save("bookstore.xml");//保存。
  30. //=================

C#操作xml文件實例***結(jié)果為:

 
 
 
  1. ﹤?xml version="1.0" encoding="gb2312"?﹥
  2. ﹤bookstore﹥
  3. ﹤book genre="fantasy" ISBN="2-3631-4"﹥
  4. ﹤title﹥Oberon's Legacy﹤/title﹥
  5. ﹤author﹥Corets, Eva﹤/author﹥
  6. ﹤price﹥5.95﹤/price﹥
  7. ﹤/book﹥
  8. ﹤book genre="update李贊紅" ISBN="2-3631-4"﹥
  9. ﹤title﹥CS從入門到精通﹤/title﹥
  10. ﹤author﹥亞勝﹤/author﹥
  11. ﹤price﹥58.3﹤/price﹥
  12. ﹤/book﹥
  13. ﹤/bookstore﹥

C#操作xml文件實例3、

刪除

 
 
 
  1. ﹤book genre="fantasy" 
  2. ISBN="2-3631-4"﹥節(jié)點的genre屬性,刪除 
  3. ﹤book genre="update李贊紅" ISBN="2-3631-4"﹥節(jié)點。
  4. XmlNodeList xnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes;
  5. foreach(XmlNode xn in xnl)
  6. {
  7. XmlElement xe=(XmlElement)xn;
  8. if(xe.GetAttribute("genre")=="fantasy")
  9. {
  10. xe.RemoveAttribute("genre");//刪除genre屬性
  11. }
  12. else if(xe.GetAttribute("genre")=="update李贊紅")
  13. {
  14. xe.RemoveAll();//刪除該節(jié)點的全部內(nèi)容
  15. }
  16. }
  17. xmlDoc.Save("bookstore.xml");
  18. //====================

C#操作xml文件實例***結(jié)果為:

 
 
 
  1. ﹤?xml version="1.0" encoding="gb2312"?﹥
  2. ﹤bookstore﹥
  3. ﹤book ISBN="2-3631-4"﹥
  4. ﹤title﹥Oberon's Legacy﹤/title﹥
  5. ﹤author﹥Corets, Eva﹤/author﹥
  6. ﹤price﹥5.95﹤/price﹥
  7. ﹤/book﹥
  8. ﹤book﹥
  9. ﹤/book﹥
  10. ﹤/bookstore﹥ 

C#操作xml文件實例4、

顯示所有數(shù)據(jù)。

 
 
 
  1. XmlNode xn=xmlDoc.SelectSingleNode("bookstore");
  2. XmlNodeList xnl=xn.ChildNodes;
  3. foreach(XmlNode xnf in xnl)
  4. {
  5. XmlElement xe=(XmlElement)xnf;
  6. Console.WriteLine(xe.GetAttribute("genre"));//顯示屬性值
  7. Console.WriteLine(xe.GetAttribute("ISBN"));
  8. XmlNodeList xnf1=xe.ChildNodes;
  9. foreach(XmlNode xn2 in xnf1)
  10. {
  11. Console.WriteLine(xn2.InnerText);//顯示子節(jié)點點文本
  12. }

C#操作xml文件實例的基本內(nèi)容就向你介紹到這里,希望對你了解和學(xué)習(xí)C#操作xml文件有所幫助。


網(wǎng)頁名稱:C#操作xml文件實例詳解
文章位置:http://www.5511xx.com/article/ccoggoh.html