1 #region 循环读取XML里的内容
2 /// <summary>
3 /// 循环读取XML里的内容
4 /// Node.HasChildNodes 判断是否有节点
5 /// </summary>
6 private void XMLopertator(string types)
7 {
8 string lujin = Application["lujin"] + "/XMLTest/XMLturba.xml";
9 XmlDocument xmlDoc = new XmlDocument();
10 xmlDoc.Load(Server.MapPath(lujin));
11 XmlNodeList Node_List = xmlDoc.SelectSingleNode("turba").ChildNodes; //获取turba节点的所有子节点
12 foreach (XmlNode Node in Node_List)
13 {
14 XmlElement xnl = (XmlElement)Node;
15 if (xnl.GetAttribute("txid") == types)
16 {
17 TelList.SelectedValue = types;//下拉框控件赋予默认值
18 if (xnl.GetAttribute("txnr") != "" || xnl.GetAttribute("txnr") != null)
19 {
20 txtel.Text = xnl.GetAttribute("txnr");//XmlElement类中GetAttribute方法取属性值
21 }
22 else
23 {
24 txtel.Text = "";
25 }
26 break;//找到跳出循环
27 }
28 }
29 }
30 #endregion 循环读取XML里的内容
31
32 #region XML文件操作添加节点
33 /// <summary>
34 /// <param name="MainNode">当前节点</param>
35 /// <param name="node">新节点</param>
36 /// <param name="content">新节点值</param>
37 /// </summary>
38 private void XMLAddElement(string types, string txtstr)
39 {
40 string lujin = Application["lujin"] + "/XMLTest/XMLturba.xml";
41 XmlDocument xmlDoc = new XmlDocument();
42 xmlDoc.Load(Server.MapPath(lujin));
43 /*
44 //创建根节点
45 XmlElement MyCustomers = xmlDoc.CreateElement("peioinfo");
46 xmlDoc.AppendChild(MyCustomers);
47 */
48 XmlNode MainNode = xmlDoc.SelectSingleNode("turba");
49 //添加子节点
50 XmlElement tongxun = xmlDoc.CreateElement("tongxun"); //添加tongxun子节点及属性
51 tongxun.SetAttribute("txid", types);
52 tongxun.SetAttribute("txnr", txtstr);
53 MainNode.AppendChild(tongxun);
54 //保存XML文档
55 xmlDoc.Save(Server.MapPath(lujin));
56 }
57 #endregion XML文件操作添加节点
58
59 #region XML文件操作修改节点
60 private void XMLEditElement(string types, string txtstr)
61 {
62 string lujin = Application["lujin"] + "/XMLTest/XMLturba.xml";
63 XmlDocument xmlDoc = new XmlDocument();
64 xmlDoc.Load(Server.MapPath(lujin));
65
66 XmlNodeList xnl = xmlDoc.SelectSingleNode("turba").ChildNodes;
67 foreach (XmlNode xn in xnl)
68 {
69 XmlElement xe = (XmlElement)xn;
70 if (xe.GetAttribute("txid") == types)//如果节点存在则改写
71 {
72 xe.SetAttribute("txnr", txtstr);
73 break;
74 }
75 }
76 xmlDoc.Save(Server.MapPath(lujin));
77 }
78 #endregion XML文件操作添加节点
79
80 #region XML文件操作删除节点
81 private void XMLDelElement(string types)
82 {
83 string lujin = Application["lujin"] + "/XMLTest/XMLturba.xml";
84 XmlDocument xmlDoc = new XmlDocument();
85 xmlDoc.Load(Server.MapPath(lujin));
86
87 XmlNodeList xnl = xmlDoc.SelectSingleNode("turba").ChildNodes;
88 foreach (XmlNode xn in xnl)
89 {
90 XmlElement xe = (XmlElement)xn;
91 if (xe.GetAttribute("txid") == types)//如果节点存在则改写
92 {
93 xe.RemoveAll();
94 break;
95 }
96 }
97 xmlDoc.Save(Server.MapPath(lujin));
98 }
99 #endregion XML文件操作删除节点
请发表评论