2  <config>
3&">

[VB.NET+XML]完成简单程序配置

80酷酷网    80kuku.com

  xml|程序 

简单的一个应用例子 ,只是简单的操作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 XmlDocument
2    Public rootnode As XmlElement
3    Public strPathConfig As String = Application.StartupPath & "\config.xml"
4    Public intNum As Int16
5    Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
6        WriteConfig()
7    End Sub
8    Private Sub Form1_Load()Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
9        '做准备工作
10        doc.Load(strPathConfig)
11        rootnode = doc.DocumentElement
12        intNum = rootnode.ChildNodes.Count - 1
13        '
14        LoadConfig()
15    End Sub
16    '添加到CheckedListBox
17    Function LoadConfig()Function LoadConfig()
18        Dim i As Int16
19        For i = 0 To intNum
20            CheckedListBox1.Items.Add(rootnode.ChildNodes(i).Attributes(0).Value, CType(rootnode.ChildNodes(i).Attributes(1).Value, Boolean))
21        Next
22    End Function
23    '确定,然后写入XML
24    Function WriteConfig()Function WriteConfig()
25        Dim i As Int16
26        Dim str As String
27        For i = 0 To intNum
28            rootnode.ChildNodes(i).Attributes(1).Value = CheckedListBox1.GetItemChecked(i).ToString
29        Next
30        doc.Save(strPathConfig)
31    End Function
32
33    Private Sub Button2_Click()Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
34        '需要清除CheckedListBox内的Items
35        CheckedListBox1.Items.Clear()
36        LoadConfig()
37    End Sub

end---

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: