|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 |
C# 添加,修改,删除Xml节点//添加xml节点 private
void AddXml(string
image, string
title) { XmlDocument xmlDoc = new
XmlDocument(); xmlDoc.Load(Server.MapPath("../flash/dati.xml")); XmlNode root = xmlDoc.SelectSingleNode("images");//查找<images> XmlElement xe1 = xmlDoc.CreateElement("thumb");//创建一个<thumb>节点 xe1.SetAttribute("displayNum", "6");//设置该节点displayNum属性 xe1.SetAttribute("separation", "5");//设置该节点separation属性 XmlElement xesub1 = xmlDoc.CreateElement("image"); xesub1.InnerText = image;//设置文本节点 xe1.AppendChild(xesub1);//添加到thumb节点中 XmlElement xesub2 = xmlDoc.CreateElement("description"); xesub2.InnerText = title; xe1.AppendChild(xesub2); root.AppendChild(xe1);//添加到<images>节点中 xmlDoc.Save(Server.MapPath("../flash/dati.xml")); } //删除节点内容 private
void DelXml(string
image) { XmlDocument xmlDoc = new
XmlDocument(); xmlDoc.Load(Server.MapPath("../flash/dati.xml")); XmlNodeList xnl = xmlDoc.SelectSingleNode("images").ChildNodes; foreach
(XmlNode xn in
xnl) { XmlElement xe = (XmlElement)xn; if
(xe.InnerText.IndexOf(image,0) >= 0) { xn.ParentNode.RemoveChild(xn); // xn.RemoveAll(); } } xmlDoc.Save(Server.MapPath("../flash/dati.xml")); } //修改foreach(XmlNode xn in
nodeList)//遍历所有子节点 { XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型 if(xe.GetAttribute("genre")=="")// 判读条件 { xe.SetAttribute("genre",newStr);//则修改该属性为newstr XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点 foreach(XmlNode xn1 in
nls)//遍历 { XmlElement xe2=(XmlElement)xn1;//转换类型 if(xe2.Name=="author")//如果找到 //判读条件 { xe2.InnerText=newText;//则修改 break;//找到退出来就可以了 } } break; } |
原文:http://www.cnblogs.com/cowkeys/p/3552532.html