zoukankan      html  css  js  c++  java
  • 添加、修改、删除XML节点代码例子

    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>
  • 相关阅读:
    实战 | 使用maven 轻松重构项目
    分布式架构高可用与高并发那些在工作中常用到的那些变态应用
    操作系统的那些灵魂概念,你弄懂了几个?
    应该没人比我更细了吧:带你深入剖析Redis分布式锁!
    我把 Spring Cloud 给拆了!带你详细了解各组件原理!
    深入浅出MySQL灵魂十连问,你真的有把握吗?
    领统Java并发半壁江山的AQS你真的懂了吗?
    不会数据结构?24张图让你彻底弄懂它,还不会你来打我!
    你了解Spring事务传播行为吗?多个方法之间调用事务如何传播?
    深入学习:三分钟快速教会你编写线程安全代码!
  • 原文地址:https://www.cnblogs.com/shiwz/p/6598879.html
Copyright © 2011-2022 走看看