C# 操作XML入门
生活随笔
收集整理的這篇文章主要介紹了
C# 操作XML入门
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 已知有一個XML文件(bookstore.xml)如下:
2
3
4 <?xmlversion="1.0"encoding="gb2312"?>
5 <bookstore>
6 <bookgenre="fantasy"ISBN="2-3631-4">
7 <title>Oberon'sLegacy</title>
8 <author>Corets,Eva</author>
9 <price>5.95</price>
10 </book>
11 </bookstore>
12
13 1、往<bookstore>節點中插入一個<book>節點:
14
15 XmlDocumentxmlDoc=newXmlDocument();
16 xmlDoc.Load("bookstore.xml");
17 XmlNoderoot=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore>
18 XmlElementxe1=xmlDoc.createElement_x("book");//創建一個<book>節點
19 xe1.SetAttribute("genre","李贊紅");//設置該節點genre屬性
20 xe1.SetAttribute("ISBN","2-3631-4");//設置該節點ISBN屬性
21
22 XmlElementxesub1=xmlDoc.createElement_x("title");
23 xesub1.InnerText="CS從入門到精通";//設置文本節點
24 xe1.AppendChild(xesub1);//添加到<book>節點中
25 XmlElementxesub2=xmlDoc.createElement_x("author");
26 xesub2.InnerText="候捷";
27 xe1.AppendChild(xesub2);
28 XmlElementxesub3=xmlDoc.createElement_x("price");
29 xesub3.InnerText="58.3";
30 xe1.AppendChild(xesub3);
31
32 root.AppendChild(xe1);//添加到<bookstore>節點中
33 xmlDoc.Save("bookstore.xml");
34
35
36 //================
37 結果為:
38
39
40
41
42 <?xmlversion="1.0"encoding="gb2312"?>
43 <bookstore>
44 <bookgenre="fantasy"ISBN="2-3631-4">
45 <title>Oberon'sLegacy</title>
46 <author>Corets,Eva</author>
47 <price>5.95</price>
48 </book>
49 <bookgenre="李贊紅"ISBN="2-3631-4">
50 <title>CS從入門到精通</title>
51 <author>候捷</author>
52 <price>58.3</price>
53 </book>
54 </bookstore>
55
56 2、修改節點:將genre屬性值為“李贊紅“的節點的genre值改為“update李贊紅”,將該節點的子節點<author>的文本修改為“亞勝”。
57
58
59 XmlNodeListnodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//獲取bookstore節點的所有子節點
60 foreach(XmlNodexninnodeList)//遍歷所有子節點
61 {
62 XmlElementxe=(XmlElement)xn;//將子節點類型轉換為XmlElement類型
63 if(xe.GetAttribute("genre")=="李贊紅")//如果genre屬性值為“李贊紅”
64 {
65 xe.SetAttribute("genre","update李贊紅");//則修改該屬性為“update李贊紅”
66
67 XmlNodeListnls=xe.ChildNodes;//繼續獲取xe子節點的所有子節點
68 foreach(XmlNodexn1innls)//遍歷
69 {
70 XmlElementxe2=(XmlElement)xn1;//轉換類型
71 if(xe2.Name=="author")//如果找到
72 {
73 xe2.InnerText="亞勝";//則修改
74 break;//找到退出來就可以了
75 }
76 }
77 break;
78 }
79 }
80
81 xmlDoc.Save("bookstore.xml");//保存。
82
83
84 //=================
85
86 最后結果為:
87
88
89 <?xmlversion="1.0"encoding="gb2312"?>
90 <bookstore>
91 <bookgenre="fantasy"ISBN="2-3631-4">
92 <title>Oberon'sLegacy</title>
93 <author>Corets,Eva</author>
94 <price>5.95</price>
95 </book>
96 <bookgenre="update李贊紅"ISBN="2-3631-4">
97 <title>CS從入門到精通</title>
98 <author>亞勝</author>
99 <price>58.3</price>
100 </book>
101 </bookstore>
102
103
104 3、刪除<bookgenre="fantasy"ISBN="2-3631-4">節點的genre屬性,刪除<bookgenre="update李贊紅"ISBN="2-3631-4">節點。
105
106
107 XmlNodeListxnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes;
108
109 foreach(XmlNodexninxnl)
110 {
111 XmlElementxe=(XmlElement)xn;
112
113
114 if(xe.GetAttribute("genre")=="fantasy")
115 {
116 xe.RemoveAttribute("genre");//刪除genre屬性
117 }
118 elseif(xe.GetAttribute("genre")=="update李贊紅")
119 {
120 xe.RemoveAll();//刪除該節點的全部內容
121 }
122 }
123 xmlDoc.Save("bookstore.xml");
124
125 //====================
126
127 最后結果為:
128
129
130 <?xmlversion="1.0"encoding="gb2312"?>
131 <bookstore>
132 <bookISBN="2-3631-4">
133 <title>Oberon'sLegacy</title>
134 <author>Corets,Eva</author>
135 <price>5.95</price>
136 </book>
137 <book>
138 </book>
139 </bookstore>
140
141
142 4、顯示所有數據。
143
144
145 XmlNodexn=xmlDoc.SelectSingleNode("bookstore");
146
147 XmlNodeListxnl=xn.ChildNodes;
148
149 foreach(XmlNodexnfinxnl)
150 {
151 XmlElementxe=(XmlElement)xnf;
152 Console.WriteLine(xe.GetAttribute("genre"));//顯示屬性值
153 Console.WriteLine(xe.GetAttribute("ISBN"));
154
155 XmlNodeListxnf1=xe.ChildNodes;
156 foreach(XmlNodexn2inxnf1)
157 {
158 Console.WriteLine(xn2.InnerText);//顯示子節點點文本
159 }
160 }
?
轉載于:https://www.cnblogs.com/jerry-liu/archive/2013/04/09/3009398.html
總結
以上是生活随笔為你收集整理的C# 操作XML入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java Io流实现图片复制
- 下一篇: 关于Delphi中DLL,BPL等无法调