简单的一个应用例子 ,只是简单的操作XML文件,在实际的应用上可以先建一个配置类保存状态。
XML文件的结构:
1
<?xml version="1.0" encoding="utf-8" ?>2
<config>3
<Function Name="a" Checked="false" />4
<Function Name="b" Checked="false" />5
<Function Name="c" Checked="false" />6
<Function Name="d" Checked="false" />7
</config>具体的程序:需要一个CheckedListBox,两个Button
1
Public doc As New XmlDocument2
Public rootnode As XmlElement3
Public strPathConfig As String = Application.StartupPath & "\config.xml"4
Public intNum As Int165
Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click6
WriteConfig()7
End Sub8
Private Sub Form1_Load()Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load9
'做准备工作10
doc.Load(strPathConfig)11
rootnode = doc.DocumentElement12
intNum = rootnode.ChildNodes.Count - 113
'14
LoadConfig()15
End Sub16
'添加到CheckedListBox17
Function LoadConfig()Function LoadConfig()18
Dim i As Int1619
For i = 0 To intNum20
CheckedListBox1.Items.Add(rootnode.ChildNodes(i).Attributes(0).Value, CType(rootnode.ChildNodes(i).Attributes(1).Value, Boolean))21
Next22
End Function23
'确定,然后写入XML24
Function WriteConfig()Function WriteConfig()25
Dim i As Int1626
Dim str As String27
For i = 0 To intNum28
rootnode.ChildNodes(i).Attributes(1).Value = CheckedListBox1.GetItemChecked(i).ToString29
Next30
doc.Save(strPathConfig)31
End Function32
33
Private Sub Button2_Click()Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click34
'需要清除CheckedListBox内的Items35
CheckedListBox1.Items.Clear()36
LoadConfig()37
End Subend---