对XML的操作
这几天在公司没事干,写了一些对XML操作的方法。今天把它放到上面,不会操作XML的朋友可以看一下。
以下是XML文件:
<?xml version="1.0" encoding="utf-8"?>
<conn>
<Conntionstring>
<conntionKey>fe</conntionKey>
<conntionValue>fafd</conntionValue>
</Conntionstring>
<Conntionstring>
<conntionKey>fsafd</conntionKey>
<conntionValue>fafd</conntionValue>
</Conntionstring>
<Conntionstring>
<conntionKey>fsafd</conntionKey>
<conntionValue>fafd</conntionValue>
</Conntionstring>
<Conntionstring>
<conntionKey>fsafd</conntionKey>
<conntionValue>fafd</conntionValue>
</Conntionstring>
</conn>
1 protected void btnAdd_ServerClick(object sender, EventArgs e)
2 {
3 //添加XML
4 XmlDocument xmldoc = new XmlDocument();
5 xmldoc.Load(Server.MapPath("work.xml"));//要添加的文件
6 XmlNode node = xmldoc.SelectSingleNode("conn");//根结点
7 XmlElement xe = xmldoc.CreateElement("Conntionstring");//库名
8 XmlElement xe1 = xmldoc.CreateElement("conntionKey");//字段名
9 xe1.InnerText = this.Text1.Value.ToString();
10 xe.AppendChild(xe1);
11 XmlElement xe2 = xmldoc.CreateElement("conntionValue");//字段名
12 xe2.InnerText = this.Text2.Value.ToString();
13 xe.AppendChild(xe2);
14 node.AppendChild(xe);
15 xmldoc.Save(Server.MapPath("work.xml"));//保存
16 }
17 protected void btnDelete_ServerClick(object sender, EventArgs e)
18 {
19 //删除XML
20 XmlDocument xmldoc = new XmlDocument();
21 xmldoc.Load(Server.MapPath("work.xml"));
22 XmlNodeList nodelist = xmldoc.SelectSingleNode("conn").ChildNodes;
23 foreach (XmlNode node in nodelist)
24 {
25 XmlElement xe = (XmlElement)node;
26 XmlNodeList _nodelist = xe.GetElementsByTagName("conntionKey");
27 if (_nodelist.Count > 0){
28 for(int i=0 ; i<_nodelist.Count ;i ++)
29 if (_nodelist[i].InnerText == this.Text1.Value.ToString()){
30 //_nodelist[i].RemoveAll();
31 node.ParentNode.RemoveChild(node);
32 //xe.RemoveAll();
33
34 }
35 }
36 }
37 xmldoc.Save(Server.MapPath("work.xml"));
38 }
39
40 protected void btnEdit_ServerClick(object sender, EventArgs e)
41 {
42 //修改XML 以conntionKey的值进行修改
43 XmlDocument xmldoc = new XmlDocument();
44 xmldoc.Load(Server.MapPath("work.xml"));
45 XmlNodeList nodelist = xmldoc.SelectSingleNode("conn").ChildNodes;
46 foreach (XmlNode node in nodelist)
47 {
48 XmlElement xe = (XmlElement)node;
49 XmlNodeList _nodelist = xe.GetElementsByTagName("conntionKey");
50 if (_nodelist.Count > 0)
51 {
52 if (_nodelist[0].InnerText == this.Text1.Value.ToString())
53 {
54 XmlNodeList nodeList2 = xe.ChildNodes;
55 foreach (XmlNode Node1 in nodeList2)
56 {
57 XmlElement xe1 = (XmlElement)Node1;
58 if (xe1.Name == "conntionValue")
59 {
60 xe1.InnerText = this.Text2.Value.ToString();
61 }
62 }
63 }
64 }
65 }
66 xmldoc.Save(Server.MapPath("work.xml"));
67
68 }
69 public void RedXml()
70 {
71 //读取XML
72 XmlDocument doc = new XmlDocument();
73 doc.Load(Server.MapPath("XMLFile.xml"));
74 //return new SqlConnection(System.Configuration.ConfigurationManager.);
75 return new SqlConnection(doc.SelectSingleNode("//ConnName[ConnKey='ConnValue']").ChildNodes.Item(1).InnerText);
76 }
2 {
3 //添加XML
4 XmlDocument xmldoc = new XmlDocument();
5 xmldoc.Load(Server.MapPath("work.xml"));//要添加的文件
6 XmlNode node = xmldoc.SelectSingleNode("conn");//根结点
7 XmlElement xe = xmldoc.CreateElement("Conntionstring");//库名
8 XmlElement xe1 = xmldoc.CreateElement("conntionKey");//字段名
9 xe1.InnerText = this.Text1.Value.ToString();
10 xe.AppendChild(xe1);
11 XmlElement xe2 = xmldoc.CreateElement("conntionValue");//字段名
12 xe2.InnerText = this.Text2.Value.ToString();
13 xe.AppendChild(xe2);
14 node.AppendChild(xe);
15 xmldoc.Save(Server.MapPath("work.xml"));//保存
16 }
17 protected void btnDelete_ServerClick(object sender, EventArgs e)
18 {
19 //删除XML
20 XmlDocument xmldoc = new XmlDocument();
21 xmldoc.Load(Server.MapPath("work.xml"));
22 XmlNodeList nodelist = xmldoc.SelectSingleNode("conn").ChildNodes;
23 foreach (XmlNode node in nodelist)
24 {
25 XmlElement xe = (XmlElement)node;
26 XmlNodeList _nodelist = xe.GetElementsByTagName("conntionKey");
27 if (_nodelist.Count > 0){
28 for(int i=0 ; i<_nodelist.Count ;i ++)
29 if (_nodelist[i].InnerText == this.Text1.Value.ToString()){
30 //_nodelist[i].RemoveAll();
31 node.ParentNode.RemoveChild(node);
32 //xe.RemoveAll();
33
34 }
35 }
36 }
37 xmldoc.Save(Server.MapPath("work.xml"));
38 }
39
40 protected void btnEdit_ServerClick(object sender, EventArgs e)
41 {
42 //修改XML 以conntionKey的值进行修改
43 XmlDocument xmldoc = new XmlDocument();
44 xmldoc.Load(Server.MapPath("work.xml"));
45 XmlNodeList nodelist = xmldoc.SelectSingleNode("conn").ChildNodes;
46 foreach (XmlNode node in nodelist)
47 {
48 XmlElement xe = (XmlElement)node;
49 XmlNodeList _nodelist = xe.GetElementsByTagName("conntionKey");
50 if (_nodelist.Count > 0)
51 {
52 if (_nodelist[0].InnerText == this.Text1.Value.ToString())
53 {
54 XmlNodeList nodeList2 = xe.ChildNodes;
55 foreach (XmlNode Node1 in nodeList2)
56 {
57 XmlElement xe1 = (XmlElement)Node1;
58 if (xe1.Name == "conntionValue")
59 {
60 xe1.InnerText = this.Text2.Value.ToString();
61 }
62 }
63 }
64 }
65 }
66 xmldoc.Save(Server.MapPath("work.xml"));
67
68 }
69 public void RedXml()
70 {
71 //读取XML
72 XmlDocument doc = new XmlDocument();
73 doc.Load(Server.MapPath("XMLFile.xml"));
74 //return new SqlConnection(System.Configuration.ConfigurationManager.);
75 return new SqlConnection(doc.SelectSingleNode("//ConnName[ConnKey='ConnValue']").ChildNodes.Item(1).InnerText);
76 }
以下是XML文件:
<?xml version="1.0" encoding="utf-8"?>
<conn>
<Conntionstring>
<conntionKey>fe</conntionKey>
<conntionValue>fafd</conntionValue>
</Conntionstring>
<Conntionstring>
<conntionKey>fsafd</conntionKey>
<conntionValue>fafd</conntionValue>
</Conntionstring>
<Conntionstring>
<conntionKey>fsafd</conntionKey>
<conntionValue>fafd</conntionValue>
</Conntionstring>
<Conntionstring>
<conntionKey>fsafd</conntionKey>
<conntionValue>fafd</conntionValue>
</Conntionstring>
</conn>