<kingeasy>
<property name="Remote.IP" value="61.144.192.91" desc="..."/>
<property name="Remote.Port" value="10000" desc="..."/>
<property name="Remote.Interval" value="10000" desc="..."/>
<property name="Card.Interval" value="1" desc="..."/>
<property name="Upload.Interval" value="10" desc="..."/>
</kingeasy>
如果XML是这种结构,程序应该怎么修改value的值?
想实现这样一个操作,主程序加载时,读取配置文件的信息,并提供一个按钮,跳到修改配置文件的页面,首先将读取出来的配置文件信息赋值到文本框中,修改提交,刚将文本的值替换相应配置文件中的value值,请问程序应该怎么写?
<property name="Remote.IP" value="61.144.192.91" desc="..."/>
<property name="Remote.Port" value="10000" desc="..."/>
<property name="Remote.Interval" value="10000" desc="..."/>
<property name="Card.Interval" value="1" desc="..."/>
<property name="Upload.Interval" value="10" desc="..."/>
</kingeasy>
如果XML是这种结构,程序应该怎么修改value的值?
想实现这样一个操作,主程序加载时,读取配置文件的信息,并提供一个按钮,跳到修改配置文件的页面,首先将读取出来的配置文件信息赋值到文本框中,修改提交,刚将文本的值替换相应配置文件中的value值,请问程序应该怎么写?
解决方案 »
- 求教: 请问WebBrowser1中的滚动条如何实现自动翻页(WebBrowser1中有打开的网页)
- 怎样使用WinExec,我想把datagrid中显示出来的的TP地址传给浏览器,并且打开对应的网站???高手帮忙
- 如何调节自定义的鼠标指针的位置
- MSHFlexgrid控件如何隐藏一列??
- YanJieBing能打开msn吗?
- 请问在哪里可以找到InstallShield for vb的???????????
- VC中用LiteZip和LiteUnzip解压Linux下的压缩包
- 快来下载 QQ神奇字符画 2008 核心源代码(VB 6.0)
- 快来抢分啊,关于打印的问题
- 给68分救命钱,关于页面设置问题,也许很简单。
- 大侠们,救命!(VB程序控制打印机钱箱问题)
- 请问如何设置EXCEL固定一列中某个单元格的内容
http://www.xuevb.net/modules/news/article.php?storyid=795
<FirstName>Rod</FirstName>
<LastName>Stephens</LastName>
</Person>
可我看的例子差不多都是这种结构的
Public Type coProperty
name As String
value As String
End Type' 缺省属性
Private m_colProps As Collection' 当前资源
Private m_colStrings As Collection'============================================================*
'方法名: getProperty
'版本: 1.0
'说明: 用于获得系统定义的参数
'参数: pID, pDefault
'返回值: String
'============================================================*
'##ModelId=3CB1B25D032D
Public Function getProperty(ByVal pID As Variant, Optional ByVal pDefault As String = "") As String
On Error GoTo errh
If m_colProps Is Nothing Then
Call loadProperties
End If
getProperty = m_colProps.Item(pID).value
Exit Function
errh:
getProperty = pDefault
End Function'============================================================*
'方法名: loadProperties
'版本: 1.0
'说明: 从系统配置中加载属性
'参数: pConfigFile
'============================================================*
Public Sub loadProperties(Optional ByVal pConfigFile As String)
On Error GoTo errh
Dim xmlDoc As MSXML2.FreeThreadedDOMDocument
Dim ElemList
Dim i
Dim msg As String
Dim sPropFile As String * 250
Set m_colProps = New Collection
Set xmlDoc = New MSXML2.FreeThreadedDOMDocument
If Len(Trim(pConfigFile)) = 0 Then
sPropFile = App.Path + "\" + "kingeasy.xml"
Else
sPropFile = Trim(pConfigFile)
If InStr(1, sPropFile, "\") = 0 Then
sPropFile = App.Path + "\" + sPropFile
End If
End If
If Not xmlDoc.Load(sPropFile) Then
With xmlDoc.parseError
msg = "code:" & .errorCode & _
Chr(10) & "Line:" & .Line & _
Chr(10) & "Cause:" & .reason & _
Chr(10) & "Desc:" & .srcText
End With
End If
Dim prop As coProperty
Set ElemList = xmlDoc.getElementsByTagName("property")
For i = 0 To (ElemList.length - 1)
prop.name = ElemList.Item(i).Attributes.Item(0).Text
prop.value = ElemList.Item(i).Attributes.Item(1).Text
m_colProps.Add prop, prop.name
Next
xmlDoc.Abort
Set xmlDoc = Nothing
Exit Sub
errh:
xmlDoc.Abort
Set m_colProps = Nothing
Set xmlDoc = Nothing
End Sub'============================================================*
'方法名: setProperty
'版本: 1.0
'说明: 设置系统属性
'参数: pPropertyName, pPropertyValue
'返回值: boolean
'============================================================*
Public Sub setProperty(ByVal pPropertyName As String, ByVal pPropertyValue As String)
On Error GoTo errh
If m_colProps Is Nothing Then
Call loadProperties
End If
m_colProps.Item(pPropertyName).value = pPropertyValue
Exit Sub
errh:
' property name not found
Dim prop As coProperty
prop.name = pPropertyName
prop.value = pPropertyValue
m_colProps.Add prop, prop.name
Resume Next
End Sub'============================================================*
'方法名: saveProperties
'版本: 1.0
'说明: 保存属性到系统配置中
'参数: pConfigFile
'返回值: boolean
'============================================================*
Public Function saveProperties(Optional ByVal pConfigFile As String) As Boolean
On Error GoTo errh
Dim xmlDoc As MSXML2.FreeThreadedDOMDocument
Dim ElemList
Dim msg As String
Dim sPropFile As String * 250
' 检查参数
If Len(Trim(pConfigFile)) = 0 Then
sPropFile = App.Path + "\" + "kingeasy.xml"
Else
sPropFile = Trim(pConfigFile)
If InStr(1, sPropFile, "\") = 0 Then
sPropFile = App.Path + "\" + sPropFile
End If
End If
If m_colProps Is Nothing Then
saveProperties = False
Exit Function
End If
Set xmlDoc = New MSXML2.FreeThreadedDOMDocument
Dim i As Integer
Dim elem, root
xmlDoc.async = False
xmlDoc.preserveWhiteSpace = True
If Not xmlDoc.Load(sPropFile) Then
With xmlDoc.parseError
msg = "code:" & .errorCode & _
Chr(10) & "Line:" & .Line & _
Chr(10) & "Cause:" & .reason & _
Chr(10) & "Desc:" & .srcText
End With
End If
Set root = xmlDoc.documentElement
Set elem = xmlDoc.createDocumentFragment
root.insertBefore elem, root.firstChild
For i = 1 To m_colProps.Count
Set elem = xmlDoc.selectSingleNode("cyberoffice/property[@name = """ & m_colProps.Item(i).name & """]")
If elem Is Nothing Then
Set elem = xmlDoc.createElement("property")
Call elem.setAttribute("name", m_colProps.Item(i).name)
Call elem.setAttribute("value", m_colProps.Item(i).value)
root.appendChild elem
Else
Call elem.setAttribute("value", m_colProps.Item(i).value)
End If
Next
xmlDoc.Save sPropFile
Set xmlDoc = Nothing
saveProperties = True
Exit Function
errh:
xmlDoc.Abort
Set xmlDoc = Nothing
saveProperties = False
End Function发段实例,大家讨论下,学习学习
请问这句话是啥意思啊,没看明白
Set elem = xmlDoc.selectSingleNode("kingeasy/property[@name = ""Remote.Port""]")
m_colProps.Item(pPropertyName).value = pPropertyValue
并没有把pPropertyValue保存到配置文件中去,请问我应该怎么将传递过来的参数保存到保存到配置文件中去?
可不可以把saveProperties方法中这段
Set root = xmlDoc.documentElement
Set elem = xmlDoc.createDocumentFragment
root.insertBefore elem, root.firstChild
For i = 1 To m_colProps.Count
Set elem = xmlDoc.selectSingleNode("cyberoffice/property[@name = """ & m_colProps.Item(i).name & """]")
If elem Is Nothing Then
Set elem = xmlDoc.createElement("property")
Call elem.setAttribute("name", m_colProps.Item(i).name)
Call elem.setAttribute("value", m_colProps.Item(i).value)
root.appendChild elem
Else
Call elem.setAttribute("value", m_colProps.Item(i).value)
End If
Next
放到setProperty中去,请问应该如何做啊,大哥帮帮忙啊
m_colProps 只是一个 Collection类型的对象,当然没保存到配置文件中。
你的代码中的流程是根据m_colProps
重新生成一个新的DOM结构,然后把这个结构保存到一个文件,你这样做也是可以的,最后保存到原来的文件中就可以了。
Public Function GetNodeValue(ByVal start_at_node As IXMLDOMNode, _
ByVal node_name As String, _
Optional ByVal default_value As String = "") As String
Dim value_node As IXMLDOMNode
Set value_node = start_at_node.selectSingleNode(".//" & node_name)
If value_node Is Nothing Then
GetNodeValue = default_value
Else
GetNodeValue = value_node.Text
End If
End Function
'从XML文档中取值.
Public Sub LoadValues() Dim xml_document As DOMDocument
Dim values_node As IXMLDOMNode
Dim i, m As Integer
' Load the document.
Set xml_document = New DOMDocument
' 取得文件的路径
m_AppPath = App.Path
If Right$(m_AppPath, 1) <> "\" Then m_AppPath = m_AppPath & "\"
xml_document.Load m_AppPath & "Values.xml"
' 如果LOAD的文件不存在,Do Nothing
If xml_document.documentElement Is Nothing Then Exit Sub
' 找到值的集合
Set values_node = xml_document.selectSingleNode("Values")
'读出保存的数据值
Form1.Text1.Text = GetNodeValue(values_node, "Remote.IP", "")
Form1.Text2.Text = GetNodeValue(values_node, "Remote.Port", "")
Form1.Text3.Text = GetNodeValue(values_node, "Remote.Interval", "") 'timer2.interval
End Sub