简单的一个应用例子 ,只是简单的操作xml文件,在实际的应用上可以先建一个配置类保存状态。
xml文件的结构:
1 <?xml version="1.0" encoding="utf-8" ?>
 <?xml version="1.0" encoding="utf-8" ?> 
2 <config>
  <config>
3 <function name="a" checked="false" />
  <function name="a" checked="false" /> 
4 <function name="b" checked="false" />
  <function name="b" checked="false" /> 
5 <function name="c" checked="false" />
  <function name="c" checked="false" /> 
6 <function name="d" checked="false" />
  <function name="d" checked="false" /> 
7 </config>
  </config>
具体的程序:需要一个checkedlistbox,两个button
 1 public doc as new xmldocument
    public doc as new xmldocument
2 public rootnode as xmlelement
    public rootnode as xmlelement
3 public strpathconfig as string = application.startuppath & "/config.xml"
    public strpathconfig as string = application.startuppath & "/config.xml"
4 public intnum as int16
    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
    private sub button1_click()sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click
6 writeconfig()
        writeconfig()
7 end sub
    end sub
8
 private sub form1_load()sub form1_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
    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)
        doc.load(strpathconfig)
11 rootnode = doc.documentelement
        rootnode = doc.documentelement
12 intnum = rootnode.childnodes.count - 1
        intnum = rootnode.childnodes.count - 1
13 '
        '
14 loadconfig()
        loadconfig()
15 end sub
    end sub
16 '添加到checkedlistbox
    '添加到checkedlistbox
17
 function loadconfig()function loadconfig()
    function loadconfig()function loadconfig()
18 dim i as int16
        dim i as int16
19 for i = 0 to intnum
        for i = 0 to intnum
20 checkedlistbox1.items.add(rootnode.childnodes(i).attributes(0).value, ctype(rootnode.childnodes(i).attributes(1).value, boolean))
            checkedlistbox1.items.add(rootnode.childnodes(i).attributes(0).value, ctype(rootnode.childnodes(i).attributes(1).value, boolean))
21 next
        next
22 end function
    end function
23 '确定,然后写入xml
    '确定,然后写入xml
24
 function writeconfig()function writeconfig()
    function writeconfig()function writeconfig()
25 dim i as int16
        dim i as int16
26 dim str as string
        dim str as string
27 for i = 0 to intnum
        for i = 0 to intnum
28 rootnode.childnodes(i).attributes(1).value = checkedlistbox1.getitemchecked(i).tostring
            rootnode.childnodes(i).attributes(1).value = checkedlistbox1.getitemchecked(i).tostring
29 next
        next
30 doc.save(strpathconfig)
        doc.save(strpathconfig)
31 end function
    end function
32
33
 private sub button2_click()sub button2_click(byval sender as system.object, byval e as system.eventargs) handles button2.click
    private sub button2_click()sub button2_click(byval sender as system.object, byval e as system.eventargs) handles button2.click
34 '需要清除checkedlistbox内的items
        '需要清除checkedlistbox内的items
35 checkedlistbox1.items.clear()
        checkedlistbox1.items.clear()
36 loadconfig()
        loadconfig()
37 end sub
    end sub
新闻热点
疑难解答
图片精选