一个读函数
readxml(fileName, itemName) as String
'返回根节点下itemName 节点的值
一个写函数
writexml(fileName,itemName,itemValue) as boolean
'将根节点下的itemName节点的值赋为 itemValue
'如果fileName不存在,则创建一个xml文件
readxml(fileName, itemName) as String
'返回根节点下itemName 节点的值
一个写函数
writexml(fileName,itemName,itemValue) as boolean
'将根节点下的itemName节点的值赋为 itemValue
'如果fileName不存在,则创建一个xml文件
a = readxml(App.Path & "\aa.xml", "itemName")
b = writexml(App.Path & "\aa.xml", "itemName", "haha")
End SubFunction readxml(fileName, itemName)
Set xmldoc = CreateObject("Msxml2.DOMDocument")
xmldoc.async = False
xmldoc.validateOnParse = False
If xmldoc.Load(fileName) = False Then
Set parseError = xmldoc.parseError
MsgBox "Load xml failure." & vbCrLf & parseError.reason
Exit Function
End If
Set iName = xmldoc.documentElement.selectSingleNode(itemName)
Set xmldoc = Nothing
Set iName = Nothing
End FunctionFunction writexml(fileName, itemName, itemValue)
Set xmldoc = CreateObject("Msxml2.DOMDocument.4.0")
Set objfso = CreateObject("Scripting.FileSystemObject")
If Not objfso.fileexists(fileName) Then
Dim pi As IXMLDOMProcessingInstruction
Set pi = xmldoc.createProcessingInstruction("xml", "version=""1.0"" encoding=""GB2312""")
xmldoc.insertBefore pi, xmldoc.childNodes.Item(0)
Dim page As IXMLDOMElement
Set page = xmldoc.createElement("root")
Set xmldoc.documentElement = page
xmldoc.save fileName
Exit Function
End If
xmldoc.async = False
xmldoc.validateOnParse = False
xmldoc.Load (fileName)
Set iName = xmldoc.documentElement.selectSingleNode(itemName)
iName.Text = itemValue
xmldoc.save fileName
Set xmldoc = Nothing
Set objfso = Nothing
End Function
Private Sub Command1_Click()
a = readxml(App.Path & "\aa.xml", "itemName")
MsgBox a
b = writexml(App.Path & "\aa.xml", "itemName", "haha")
MsgBox b
End SubFunction readxml(fileName, itemName) As String
Set xmldoc = CreateObject("Msxml2.DOMDocument")
xmldoc.async = False
xmldoc.validateOnParse = False
If xmldoc.Load(fileName) = False Then
Set parseError = xmldoc.parseError
MsgBox "Load xml failure." & vbCrLf & parseError.reason
Exit Function
End If
Set iName = xmldoc.documentElement.selectSingleNode(itemName)
readxml = iName.Text
Set xmldoc = Nothing
Set iName = Nothing
End FunctionFunction writexml(fileName, itemName, itemValue) As Boolean
Set xmldoc = CreateObject("Msxml2.DOMDocument.4.0")
Set objfso = CreateObject("Scripting.FileSystemObject")
If Not objfso.fileexists(fileName) Then
Dim pi As IXMLDOMProcessingInstruction
Set pi = xmldoc.createProcessingInstruction("xml", "version=""1.0"" encoding=""GB2312""")
xmldoc.insertBefore pi, xmldoc.childNodes.Item(0)
Dim page As IXMLDOMElement
Set page = xmldoc.createElement("root")
Set xmldoc.documentElement = page
xmldoc.save fileName
writexml = False
Exit Function
End If
xmldoc.async = False
xmldoc.validateOnParse = False
xmldoc.Load (fileName)
Set iName = xmldoc.documentElement.selectSingleNode(itemName)
iName.Text = itemValue
xmldoc.save fileName
writexml = True
Set xmldoc = Nothing
Set objfso = Nothing
End Function