博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
winform 中xml简单的创建和读取
阅读量:7256 次
发布时间:2019-06-29

本文共 3855 字,大约阅读时间需要 12 分钟。


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

你可能感兴趣的文章
杨老师课堂_Java教程第五篇之函数运用
查看>>
全球区块链游戏行业研究报告——BB财经
查看>>
Confluence 6 为翻译显示用户界面的键(Key)名称
查看>>
Mozilla Firefox 67.0 Beta 8 (Quantum) 发布
查看>>
重拾Java(1)-基本数据类型与字面值
查看>>
Confluence 6 配置 Office 转换器
查看>>
大型分布式C++框架《四:netio之请求包中转站 上》
查看>>
一文带你了解 Faster R-CNN
查看>>
聊聊数据库事务的四特性(ACID)
查看>>
07.源码阅读(ClassLoader类的加载机制)
查看>>
Confluence 6 SQL Server 输入你的数据库细节
查看>>
正则表达式
查看>>
深入Android消息机制
查看>>
rds 签名机制事例及补充说明
查看>>
第十七章:掌握网格(七)
查看>>
POJ 1804
查看>>
Bioconductor的地基--IRanges
查看>>
UML类图关系大全
查看>>
Android Data Binding
查看>>
CSS:hover选择器
查看>>