.net操作xml文件(新增.修改,删除,读取) 转

今天有个需求需要操作xml节点.突然见遗忘了许多.上网看了些资料.才整出来.脑袋真不够用.在这里把我找到的资料共享一下.方便以后使用.本文属于网摘/
1 一、简单介绍
2 using System.Xml;
3 //初始化一个xml实例

4 XmlDocument xml=new XmlDocument();
5 //导入指定xml文件

6 xml.Load(path);
7 xml.Load(HttpContext.Current.Server.MapPath("~/file/bookstore.xml"));
8 //指定一个节点

9 XmlNode root=xml.SelectSingleNode("/root");
10 //获取节点下所有直接子节点

11 XmlNodeList childlist=root.ChildNodes;
12 //判断该节点下是否有子节点

13 root.HasChildNodes;
14 //获取同名同级节点集合

15 XmlNodeList nodelist=xml.SelectNodes("/Root/News");
16 //生成一个新节点

17 XmlElement node=xml.createElement_x("News");
18 //将节点加到指定节点下,作为其子节点

19 root.AppendChild(node);
20 //将节点加到指定节点下某个子节点前

21 root.InsertBefore(node,root.ChildeNodes[i]);
22 //为指定节点的新建属性并赋值

23 node.SetAttribute("id","11111");
24 //为指定节点添加子节点

25 root.AppendChild(node);
26 //获取指定节点的指定属性值

27 string id=node.Attributes["id"].Value;
28 //获取指定节点中的文本

29 string content=node.InnerText;
30 //保存XML文件

31 string path=Server.MapPath("~/file/bookstore.xml");
32 xml.Save(path);
33 //or use :xml.Save(HttpContext.Current.Server.MapPath("~/file/bookstore.xml")); 

34 二、具体实例
35 在C#.net中如何操作XML
36 需要添加的命名空间:
37 using System.Xml;
38 定义几个公共对象:
39 XmlDocument xmldoc ;
40 XmlNode xmlnode ;
41 XmlElement xmlelem ;
42 1,创建到服务器同名目录下的xml文件:
43 
44 方法一:
45 xmldoc = new XmlDocument ( ) ;
46 //加入XML的声明段落,<?xml version="1.0" encoding="gb2312"?>

47 XmlDeclaration xmldecl;
48 xmldecl = xmldoc.CreateXmlDeclaration("1.0","gb2312",null);
49 xmldoc.AppendChild ( xmldecl);
50 //加入一个根元素

51 xmlelem = xmldoc.createElement_x ( "" , "Employees" , "" ) ;
52 xmldoc.AppendChild ( xmlelem ) ;
53 //加入另外一个元素

54 for(int i=1;i<3;i++)
55 {
56 XmlNode root=xmldoc.SelectSingleNode("Employees");//查找<Employees> 

57 XmlElement xe1=xmldoc.createElement_x("Node");//创建一个<Node>节点 
58 xe1.SetAttribute("genre","李赞红");//设置该节点genre属性 
59 xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性
60 XmlElement xesub1=xmldoc.createElement_x("title"); 
61 xesub1.InnerText="CS从入门到精通";//设置文本节点 

62 xe1.AppendChild(xesub1);//添加到<Node>节点中 
63 XmlElement xesub2=xmldoc.createElement_x("author"); 
64 xesub2.InnerText="候捷"; 
65 xe1.AppendChild(xesub2); 
66 XmlElement xesub3=xmldoc.createElement_x("price"); 
67 xesub3.InnerText="58.3"; 
68 xe1.AppendChild(xesub3);
69 root.AppendChild(xe1);//添加到<Employees>节点中 

70 }
71 //保存创建好的XML文档

72 xmldoc.Save ( Server.MapPath("data.xml") ) ;
73//////////////////////////////////////////////////////////////////////////////////////

74 结果:在同名目录下生成了名为data.xml的文件,内容如下,
75 <?xml version="1.0" encoding="gb2312"?>
76 <Employees>
77 <Node genre="李赞红" ISBN="2-3631-4">
78 <title>CS从入门到精通</title>
79 <author>候捷</author>
80 <price>58.3</price>
81 </Node>
82 <Node genre="李赞红" ISBN="2-3631-4">
83 <title>CS从入门到精通</title>
84 <author>候捷</author>
85 <price>58.3</price>
86 </Node>
87 </Employees>
88 
89 方法二:
90 XmlTextWriter xmlWriter;
91 string strFilename = Server.MapPath("data1.xml") ;
92 xmlWriter = new XmlTextWriter(strFilename,Encoding.Default);//创建一个xml文档

93 xmlWriter.Formatting = Formatting.Indented;
94 xmlWriter.WriteStartDocument();
95 xmlWriter.WriteStartElement("Employees");
96 xmlWriter.WriteStartElement("Node");
97 xmlWriter.WriteAttributeString("genre","李赞红");
98 xmlWriter.WriteAttributeString("ISBN","2-3631-4");
99 xmlWriter.WriteStartElement("title");
100 xmlWriter.WriteString("CS从入门到精通");
101 xmlWriter.WriteEndElement();
102 xmlWriter.WriteStartElement("author");
103 xmlWriter.WriteString("候捷");
104 xmlWriter.WriteEndElement();
105 xmlWriter.WriteStartElement("price");
106 xmlWriter.WriteString("58.3");
107 xmlWriter.WriteEndElement();
108 xmlWriter.WriteEndElement();
109 xmlWriter.Close();
110//////////////////////////////////////////////////////////////////////////////////////

111 结果:
112 <?xml version="1.0" encoding="gb2312"?>
113 <Employees>
114 <Node genre="李赞红" ISBN="2-3631-4">
115 <title>CS从入门到精通</title>
116 <author>候捷</author>
117 <price>58.3</price>
118 </Node>
119 </Employees>
120 2,添加一个结点:
121 XmlDocument xmlDoc=new XmlDocument(); 
122 xmlDoc.Load(Server.MapPath("data.xml")); 
123 XmlNode root=xmlDoc.SelectSingleNode("Employees");//查找<Employees> 

124 XmlElement xe1=xmlDoc.createElement_x("Node");//创建一个<Node>节点 
125 xe1.SetAttribute("genre","张三");//设置该节点genre属性 
126 xe1.SetAttribute("ISBN","1-1111-1");//设置该节点ISBN属性
127 XmlElement xesub1=xmlDoc.createElement_x("title"); 
128 xesub1.InnerText="C#入门帮助";//设置文本节点 

129 xe1.AppendChild(xesub1);//添加到<Node>节点中 
130 XmlElement xesub2=xmlDoc.createElement_x("author"); 
131 xesub2.InnerText="高手"; 
132 xe1.AppendChild(xesub2); 
133 XmlElement xesub3=xmlDoc.createElement_x("price"); 
134 xesub3.InnerText="158.3"; 
135 xe1.AppendChild(xesub3);
136 root.AppendChild(xe1);//添加到<Employees>节点中 

137 xmlDoc.Save ( Server.MapPath("data.xml") );
138//////////////////////////////////////////////////////////////////////////////////////

139 结果:在xml原有的内容里添加了一个结点,内容如下,
140 <?xml version="1.0" encoding="gb2312"?>
141 <Employees>
142 <Node genre="李赞红" ISBN="2-3631-4">
143 <title>CS从入门到精通</title>
144 <author>候捷</author>
145 <price>58.3</price>
146 </Node>
147 <Node genre="李赞红" ISBN="2-3631-4">
148 <title>CS从入门到精通</title>
149 <author>候捷</author>
150 <price>58.3</price>
151 </Node>
152 <Node genre="张三" ISBN="1-1111-1">
153 <title>C#入门帮助</title>
154 <author>高手</author>
155 <price>158.3</price>
156 </Node>
157 </Employees>
158 3,修改结点的值(属性和子结点):
159 XmlDocument xmlDoc=new XmlDocument(); 
160 xmlDoc.Load( Server.MapPath("data.xml") );
161 XmlNodeList nodeList=xmlDoc.SelectSingleNode("Employees").ChildNodes;//获取Employees节点的所有子节点

162 foreach(XmlNode xn in nodeList)//遍历所有子节点 
163 { 
164 XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型 

165 if(xe.GetAttribute("genre")=="张三")//如果genre属性值为“张三” 
166 { 
167 xe.SetAttribute("genre","update张三");//则修改该属性为“update张三”

168 XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点 
169 foreach(XmlNode xn1 in nls)//遍历 
170 { 
171 XmlElement xe2=(XmlElement)xn1;//转换类型 

172 if(xe2.Name=="author")//如果找到 
173 { 
174 xe2.InnerText="亚胜";//则修改

175 } 
176 } 
177 } 
178 } 
179 xmlDoc.Save( Server.MapPath("data.xml") );//保存。

180//////////////////////////////////////////////////////////////////////////////////////
181 结果:将原来的所有结点的信息都修改了,xml的内容如下,
182 <?xml version="1.0" encoding="gb2312"?>
183 <Employees>
184 <Node genre="李赞红" ISBN="2-3631-4">
185 <title>CS从入门到精通</title>
186 <author>候捷</author>
187 <price>58.3</price>
188 </Node>
189 <Node genre="李赞红" ISBN="2-3631-4">
190 <title>CS从入门到精通</title>
191 <author>候捷</author>
192 <price>58.3</price>
193 </Node>
194 <Node genre="update张三" ISBN="1-1111-1">
195 <title>C#入门帮助</title>
196 <author>亚胜</author>
197 <price>158.3</price>
198 </Node>
199 </Employees>
200 4,修改结点(添加结点的属性和添加结点的自结点):
201 XmlDocument xmlDoc=new XmlDocument(); 
202 xmlDoc.Load( Server.MapPath("data.xml") );
203 XmlNodeList nodeList=xmlDoc.SelectSingleNode("Employees").ChildNodes;//获取Employees节点的所有子节点

204 foreach(XmlNode xn in nodeList) 
205 { 
206 XmlElement xe=(XmlElement)xn; 
207 xe.SetAttribute("test","111111");
208 XmlElement xesub=xmlDoc.createElement_x("flag"); 
209 xesub.InnerText="1"; 
210 xe.AppendChild(xesub); 
211 } 
212 xmlDoc.Save( Server.MapPath("data.xml") );
213//////////////////////////////////////////////////////////////////////////////////////

214 结果:每个结点的属性都添加了一个,子结点也添加了一个,内容如下,
215 <?xml version="1.0" encoding="gb2312"?>
216 <Employees>
217 <Node genre="李赞红" ISBN="2-3631-4" test="111111">
218 <title>CS从入门到精通</title>
219 <author>候捷</author>
220 <price>58.3</price>
221 <flag>1</flag>
222 </Node>
223 <Node genre="李赞红" ISBN="2-3631-4" test="111111">
224 <title>CS从入门到精通</title>
225 <author>候捷</author>
226 <price>58.3</price>
227 <flag>1</flag>
228 </Node>
229 <Node genre="update张三" ISBN="1-1111-1" test="111111">
230 <title>C#入门帮助</title>
231 <author>亚胜</author>
232 <price>158.3</price>
233 <flag>1</flag>
234 </Node>
235 </Employees>
236 5,删除结点中的某一个属性:
237 XmlDocument xmlDoc=new XmlDocument(); 
238 xmlDoc.Load( Server.MapPath("data.xml") ); 
239 XmlNodeList xnl=xmlDoc.SelectSingleNode("Employees").ChildNodes; 
240 foreach(XmlNode xn in xnl) 
241 { 
242 XmlElement xe=(XmlElement)xn; 
243 xe.RemoveAttribute("genre");//删除genre属性

244 XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点 
245 foreach(XmlNode xn1 in nls)//遍历 
246 { 
247 XmlElement xe2=(XmlElement)xn1;//转换类型 

248 if(xe2.Name=="flag")//如果找到 
249 { 
250 xe.RemoveChild(xe2);//则删除

251 } 
252 } 
253 } 
254 xmlDoc.Save( Server.MapPath("data.xml") );
255//////////////////////////////////////////////////////////////////////////////////////]

256 结果:删除了结点的一个属性和结点的一个子结点,内容如下,
257 <?xml version="1.0" encoding="gb2312"?>
258 <Employees>
259 <Node ISBN="2-3631-4" test="111111">
260 <title>CS从入门到精通</title>
261 <author>候捷</author>
262 <price>58.3</price>
263 </Node>
264 <Node ISBN="2-3631-4" test="111111">
265 <title>CS从入门到精通</title>
266 <author>候捷</author>
267 <price>58.3</price>
268 </Node>
269 <Node ISBN="1-1111-1" test="111111">
270 <title>C#入门帮助</title>
271 <author>亚胜</author>
272 <price>158.3</price>
273 </Node>
274 </Employees>
275 6,删除结点:
276 XmlDocument xmlDoc=new XmlDocument(); 
277 xmlDoc.Load( Server.MapPath("data.xml") ); 
278 XmlNode root=xmlDoc.SelectSingleNode("Employees");
279 XmlNodeList xnl=xmlDoc.SelectSingleNode("Employees").ChildNodes; 
280 for(int i=0;i<xnl.Count;i++)
281 {
282 XmlElement xe=(XmlElement)xnl.Item(i); 
283 if(xe.GetAttribute("genre")=="张三") 
284 { 
285 root.RemoveChild(xe);
286 if(i<xnl.Count)i=i-1;
287 } 
288 }
289 xmlDoc.Save( Server.MapPath("data.xml") );
290//////////////////////////////////////////////////////////////////////////////////////]

291 结果:删除了符合条件的所有结点,原来的内容:
292 <?xml version="1.0" encoding="gb2312"?>
293 <Employees>
294 <Node genre="李赞红" ISBN="2-3631-4">
295 <title>CS从入门到精通</title>
296 <author>候捷</author>
297 <price>58.3</price>
298 </Node>
299 <Node genre="李赞红" ISBN="2-3631-4">
300 <title>CS从入门到精通</title>
301 <author>候捷</author>
302 <price>58.3</price>
303 </Node>
304 <Node genre="张三" ISBN="1-1111-1">
305 <title>C#入门帮助</title>
306 <author>高手</author>
307 <price>158.3</price>
308 </Node>
309 <Node genre="张三" ISBN="1-1111-1">
310 <title>C#入门帮助</title>
311 <author>高手</author>
312 <price>158.3</price>
313 </Node>
314 </Employees>
315 删除后的内容:
316 <?xml version="1.0" encoding="gb2312"?>
317 <Employees>
318 <Node genre="李赞红" ISBN="2-3631-4">
319 <title>CS从入门到精通</title>
320 <author>候捷</author>
321 <price>58.3</price>
322 </Node>
323 <Node genre="李赞红" ISBN="2-3631-4">
324 <title>CS从入门到精通</title>
325 <author>候捷</author>
326 <price>58.3</price>
327 </Node>
328 </Employees>
329 7,按照文本文件读取xml
330 System.IO.StreamReader myFile =new 
331 System.IO.StreamReader(Server.MapPath("data.xml"),System.Text.Encoding.Default);
332 //注意System.Text.Encoding.Default

333 string myString = myFile.ReadToEnd();//myString是读出的字符串
334 myFile.Close();
335 
336 三、高级应用 
337 
338 <aaa>
339 <bb>something</bb>
340 <cc>something</cc>
341 </aaa>
342 
343 <aaa>
344 <add key="123" value="321"/>
345 </aaa>
346 
347 DS.ReadXml("your xmlfile name");
348 Container.DataItem("bb");
349 Container.DataItem("cc");
350 DS.ReadXmlSchema("your xmlfile name");
351 
352 
353 <aaa>
354 <add key="123" value="321"/>
355 </aaa>
356 如果我要找到123然后取到321应该怎么写呢?
357 
358 using System.XML;
359 XmlDataDocument xmlDoc = new System.Xml.XmlDataDocument();
360 xmlDoc.Load(@"c:\Config.xml");
361 XmlElement elem = xmlDoc.GetElementById("add");
362 string str = elem.Attributes["value"].Value
363 
364 
365 
366 
--------------------------------------------------------------------
367 
<?xml version="1.0" encoding="utf-8" ?>
368 
<configuration>
369 
<appSettings>
370 
<ConnectionString>Data Source=yf; user id=ctm_dbo;password=123</ConnectionString> 
371 
</appSettings>
372 
</configuration>
373 
--------------------------------------------------------------------------
374 
XmlDocument doc = new XmlDocument();
375 
doc.Load(strXmlName);
376 

377 
XmlNode node=doc.SelectSingleNode("/configuration/appSettings/ConnectionString");
378 
if(node!=null)
379 
{
380 
string k1=node.Value; //null
381 
string k2=node.InnerText;//Data Source=yf; user id=ctm_dbo;password=123
382 
string k3=node.InnerXml;//Data Source=yf; user id=ctm_dbo;password=123
383 
node=null;
384 
}
385 

386 
********************************************************************
387 
<?xml version="1.0" encoding="utf-8" ?>
388 
<configuration>
389 
<appSettings>
390 
<add key="ConnectionString" value="Data Source=yf; user id=ctm_dbo;password=123" /> 
391 
</appSettings>
392 
</configuration>
393 
**--------------------------------------------------------------------**
394 
XmlNode node=doc.SelectSingleNode("/configuration/appSettings/add");
395 
if(node!=null)
396 
{
397 
string k=node.Attributes["key"].Value;
398 
string v=node.Attributes["value"].Value;
399 
node=null;
400 
}
401 
*--------------------------------------------------------------------*
402 
XmlNode node=doc.SelectSingleNode("/configuration/appSettings/add");
403 
if(node!=null)
404 
{
405 
XmlNodeReader nr=new XmlNodeReader(node);
406 
nr.MoveToContent();
407 
//检查当前节点是否是内容节点。如果此节点不是内容节点,则读取器向前跳至下一个内容节点或文件结尾。
408 
nr.MoveToAttribute("value");
409 
string s=nr.Value;
410 
node=null;
411 }
posted @ 2013-07-22 13:11  日月当空  阅读(1176)  评论(0编辑  收藏  举报