winform 中xml简单的创建和读取


ContractedBlock.gif ExpandedBlockStart.gif View Code
 
   
1 // 修改栏目订单的颜色
2   private void btnfunColor_Click( object sender, EventArgs e)
3 {
4 bool isExsit = true ;
5 ColorDialog fontcolor = new ColorDialog();
6 if (fontcolor.ShowDialog() == DialogResult.OK)
7 {
8 if ( ! System.IO.Directory.Exists(xpath))
9 {
10 System.IO.Directory.CreateDirectory(xpath);
11 }
12 if (System.IO.File.Exists(filefullname))
13 {
14 // 已经存在改文件,判断改文件是是否存在该节点,如果存在。则修改改节点。如果不存在,则创建一个新的节点
15 XmlDocument xmldoc = new XmlDocument();
16 xmldoc.Load(filefullname);
17 XmlNode xm = xmldoc.SelectSingleNode( " First " );
18 XmlNodeList xmlNotes = xm.ChildNodes;
19 if (xmlNotes.Count > 0 ) // 存在该节点,修改该节点
20 {
21 foreach (XmlNode xml in xmlNotes)
22 {
23 XmlNode xmNod = xml.SelectSingleNode( " Third1 " );
24
25 if (((XmlElement)xmNod).GetAttribute( " Name " ) == cmbFunInfo.Text)
26 {
27 isExsit = false ;
28 // 重新赋值
29 ((XmlElement)xmNod).InnerText = fontcolor.Color.ToArgb().ToString();
30 break ;
31 }
32 }
33 }
34 if (isExsit)
35 {
36 XmlElement xmlNode = xmldoc.CreateElement( " Second " );
37 xm.AppendChild(xmlNode);
38 // 添加节点
39 XmlElement element1 = xmldoc.CreateElement( " Third1 " );
40 element1.SetAttribute( " Name " , cmbFunInfo.Text);
41 element1.SetAttribute( " ID " , cmbFunInfo.SelectedValue.ToString());
42 element1.InnerText = fontcolor.Color.ToArgb().ToString();
43 xmlNode.AppendChild(element1);
44 }
45 xmldoc.Save(filefullname);
46 }
47 else
48 {
49 XmlDocument doc = new XmlDocument();
50 XmlDeclaration dec = doc.CreateXmlDeclaration( " 1.0 " , " GB2312 " , null );
51 doc.AppendChild(dec);
52 // 创建一个根节点(一级)
53 XmlElement root = doc.CreateElement( " First " );
54 doc.AppendChild(root);
55 // 创建节点(二级)
56 XmlNode node = doc.CreateElement( " Seconde " );
57 root.AppendChild(node);
58 // 创建节点(三级)
59 XmlElement element1 = doc.CreateElement( " Third1 " );
60 element1.SetAttribute( " Name " , cmbFunInfo.Text);
61 element1.SetAttribute( " ID " , cmbFunInfo.SelectedValue.ToString());
62 element1.InnerText = fontcolor.Color.ToArgb().ToString();
63 node.AppendChild(element1);
64 doc.Save(filefullname);
65 }
66 }
67 }
68 // 循环得到颜色的值
69 public Color GetColor( int ID)
70 {
71 Color strColors = Color.Black;
72 XmlDocument doc = new XmlDocument();
73 doc.Load(filefullname);
74 foreach (XmlNode item in doc.SelectNodes( " /First " ))
75 {
76 XmlNodeList xmlNotes = item.ChildNodes;
77 if (xmlNotes.Count > 0 )
78 {
79 foreach (XmlNode xml in xmlNotes)
80 {
81 XmlNode xmNod = xml.SelectSingleNode( " Third1 " );
82 if (ID == Convert.ToInt32(((XmlElement)xmNod).GetAttribute( " ID " )))
83 {
84 // 得到color值
85 strColors = Color.FromArgb(Convert.ToInt32(xmNod.InnerText.Trim()));
86 break ;
87 }
88 }
89 }
90 }
91 return strColors;
92 }



ContractedBlock.gif ExpandedBlockStart.gif View Code
 
   
1 private string xpath = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @" \BXYZT " ;
2 // Xml文件保存文件夹名称
3 private string filefullname = null ;

xml文件如下

ContractedBlock.gif ExpandedBlockStart.gif View Code
 
   
1 <? xml version = " 1.0 " encoding = " GB2312 " ?>
2 < First >
3 < Seconde >
4 < Third1 Name = " QQ币充值2 " ID = " 25 " >- 65536 </ Third1 >
5 </ Seconde >
6 < Second >
7 < Third1 Name = " 中国水费 " ID = " 14 " >- 8388353 </ Third1 >
8 </ Second >
9 < Second >
10 < Third1 Name = " 中国电力 " ID = " 12 " >- 8388353 </ Third1 >
11 </ Second >
12 < Second >
13 < Third1 Name = " 中国燃气 " ID = " 13 " >- 65408 </ Third1 >
14 </ Second >
15 < Second >
16 < Third1 Name = " 联通固话 " ID = " 5 " >- 256 </ Third1 >
17 </ Second >
18 < Second >
19 < Third1 Name = " 电信固话 " ID = " 6 " >- 16711681 </ Third1 >
20 </ Second >
21 < Second >
22 < Third1 Name = " 中国电信 " ID = " 3 " >- 16711808 </ Third1 >
23 </ Second >
24 < Second >
25 < Third1 Name = " 全国游戏2 " ID = " 26 " >- 8323328 </ Third1 >
26 </ Second >
27 < Second >
28 < Third1 Name = " QQ币充值 " ID = " 10 " >- 65536 </ Third1 >
29 </ Second >
30 </ First >

转载于:https://www.cnblogs.com/wsl2011/archive/2011/06/22/2087589.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值