添加、修改、刪除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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM