游戏技术文章

添加、修改、删除XML节点代码例子

时间:2017-3-22 11:47:01  作者:棋牌资源网  来源:棋牌资源网  查看:6817  评论:0
内容摘要:version="1.0" encoding="gb2312"?> 02. 03. version="1.0" encoding="gb2312"?> 02. <bookstore> 03. <book genre="fantasy" isbn="2-3631-4"> 04. <title>Oberons Legacy</title> 05. <author>Corets, Eva</author> 06. <price>5.95</price> 07. </book genre="fantasy" isbn="2-3631-4"> 08. </bookstore> 09. 10. 1、往<bookstore>节点中插入一个<book>节点: 11. XmlDocument xmlDoc=new XmlDocument(); 12. xmlDoc.Load("bookstore.xml"); 13. XmlNode root=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore> 14. XmlElement xe1=xmlDoc.CreateElement("book");//创建一个<book>节点 15. xe1.SetAttribute("genre","李铁锤");//设置该节点genre属性 16. xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性 17. 18. XmlElement xesub1=xmlDoc.CreateElement("title"); 19. xesub1.InnerText="CS从入门到精通";//设置文本节点 20. xe1.AppendChild(xesub1);//添加到<book>节点中 21. XmlElement xesub2=xmlDoc.CreateElement("author"); 22. xesub2.InnerText="候捷"; 23. xe1.AppendChild(xesub2); 24. XmlElement xesub3=xmlDoc.CreateElement("price"); 25. xesub3.InnerText="58.3"; 26. xe1.AppendChild(xesub3); 27. 28. root.AppendChild(xe1);//添加到<bookstore>节点中 29. xmlDoc.Save("bookstore.xml"); 30. //=============================================== 31. 结果为: 32. <!--?xml version="1.0" encoding="gb2312"?--> 33. <bookstore> 34. <book genre="fantasy" isbn="2-3631-4"> 35. <title>Oberons Legacy</title> 36. <author>Corets, Eva</author> 37. <price>5.95</price> 38. </book genre="fantasy" isbn="2-3631-4"> 39. <book genre="李铁锤" isbn="2-3631-4"> 40. <title>CS从入门到精通</title> 41. <author>候捷</author> 42. <price>58.3</price> 43. </book genre="李铁锤" isbn="2-3631-4"> 44. </bookstore> 45. 46. 2、修改节点:将genre属性值为“李铁锤“的节点的genre值改为“update李铁锤”,将该节点的子节点<author>的文本修改为“亚胜”。 47. XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点 48. foreach(XmlNode xn in nodeList)//遍历所有子节点 49. { 50. XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型 51. if(xe.GetAttribute("genre")=="李铁锤")//如果genre属性值为“李铁锤” 52. { 53. xe.SetAttribute("genre","update李铁锤");//则修改该属性为“update李铁锤” 54. 55. XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点 56. foreach(XmlNode xn1 in nls)//遍历 57. { 58. XmlElement xe2=(XmlElement)xn1;//转换类型 59. if(xe2.Name=="author")//如果找到 60. { 61. xe2.InnerText="亚胜";//则修改 62. break;//找到退出来就可以了 63. } 64. } 65. break; 66. } 67. } 68. 69. xmlDoc.Save("bookstore.xml");//保存。 70. //================================================== 71. 最后结果为: 72. <!--?xml version="1.0" encoding="gb2312"?--> 73. <bookstore> 74. <book genre="fantasy" isbn="2-3631-4"> 75. <title>Oberons Legacy</title> 76. <author>Corets, Eva</author> 77. <price>5.95</price> 78. </book genre="fantasy" isbn="2-3631-4"> 79. <book genre="update李铁锤" isbn="2-3631-4"> 80. <title>CS从入门到精通</title> 81. <author>亚胜</author> 82. <price>58.3</price> 83. </book genre="update李铁锤" isbn="2-3631-4"> 84. </bookstore> 85. 86. 3、删除 <book genre="fantasy" isbn="2-3631-4">节点的genre属性,删除 <book genre="update李铁锤" isbn="2-3631-4">节点。 87. XmlNodeList xnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes; 88. 89. foreach(XmlNode xn in xnl) 90. { 91. XmlElement xe=(XmlElement)xn; 92. if(xe.GetAttribute("genre")=="fantasy") 93. { 94. xe.RemoveAttribute("genre");//删除genre属性 95. } 96. else if(xe.GetAttribute("genre")=="update李铁锤") 97. { 98. xe.RemoveAll();//删除该节点的全部内容 99. } 100. } 101. xmlDoc.Save("bookstore.xml"); 102. //=========================================== 103. 最后结果为: 104. <!--?xml version="1.0" encoding="gb2312"?--> 105. <bookstore> 106. <book isbn="2-3631-4"> 107. <title>Oberons Legacy</title> 108. <author>Corets, Eva</author> 109. <price>5.95</price> 110. </book isbn="2-3631-4"> 111. <book> 112. </book> 113. </bookstore> 114. 115. 4、显示所有数据。 116. XmlNode xn=xmlDoc.SelectSingleNode("bookstore"); 117. 118. XmlNodeList xnl=xn.ChildNodes; 119. 120. foreach(XmlNode xnf in xnl) 121. { 122. XmlElement xe=(XmlElement)xnf; 123. Console.WriteLine(xe.GetAttribute("genre"));//显示属性值 124. Console.WriteLine(xe.GetAttribute("ISBN")); 125. 126. XmlNodeList xnf1=xe.ChildNodes; 127. foreach(XmlNode xn2 in xnf1) 128. { 129. Console.WriteLine(xn2.InnerText);//显示子节点点文本 130. } 131. } </book genre="update李铁锤" isbn="2-3631-4"></book genre="fantasy" isbn="2-3631-4"></author></bookstore></book></book></bookstore></book></bookstore>
标签:添加修改删除XML节点代码例子 

欢迎加入VIP,【VIP售价:只要288元永久VIP会员】畅享商业棋牌游戏程序下载,点击开通!

下载说明


☉本站所有源码和资源均由站长亲自测试-绝对保证都可以架设,运营!
☉如源码和资源有损坏或所有链接均不能下载,请告知管理员,

☉本站软件和源码大部分为站长独资,资源购买和收集,放心下载!

☉唯一站长QQ:1004003180  [人格担保-本站注重诚信!]

☉购买建议E-mail:1004003180@qq.com   源码收购 E-mail:1004003180@qq.com    

☉本站文件解压密码  【文章内都自带解压密码,每个密码不同!】


本站提供的所有源码,均来源站长提供,仅学习交流 浙ICP备09009969号

由此产生不良后果和法律责任与本站无关,如果侵犯了您的版权,请来信告知 1004003180@qq.com 将及时更正和删除! 

Copyright © 2008-2024 棋牌资源网,你身边的棋牌资源下载站    All Rights Reserved