vb修改config(xml)文件
config文件文件名称为****.config,具体内容如下:<?xmlversion="1.0"?><BaseConfigInfoxmlns:xsi="http://...
config文件文件名称为****.config,具体内容如下:
<?xml version="1.0"?>
<BaseConfigInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Dbconnectstring>server=.;database=HENGDA;uid=sa;pwd=sa123.*;</Dbconnectstring>
<Tableprefix>bos_</Tableprefix>
<Dbtype>SqlServer</Dbtype>
</BaseConfigInfo>
我需要的是,使用VB修改Dbconnectstring这个节点的内容。
小弟资质驽钝,对VB基本没什么了解,但是需要用这个来修改一下Dbconnectstring节点内容。
不需要用.net或者VS来写,我要用的就是单纯的***.vbs就可以了
因为要不依附于.netframework运行环境,所以我就不能使用c#来写了。这就让我很为难了。
在网上我也看了好多关于vb操作xml的的信息,但是看不太明白。或者说,基本没看懂……所以,请各位大神相助。先在此谢过! 展开
<?xml version="1.0"?>
<BaseConfigInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Dbconnectstring>server=.;database=HENGDA;uid=sa;pwd=sa123.*;</Dbconnectstring>
<Tableprefix>bos_</Tableprefix>
<Dbtype>SqlServer</Dbtype>
</BaseConfigInfo>
我需要的是,使用VB修改Dbconnectstring这个节点的内容。
小弟资质驽钝,对VB基本没什么了解,但是需要用这个来修改一下Dbconnectstring节点内容。
不需要用.net或者VS来写,我要用的就是单纯的***.vbs就可以了
因为要不依附于.netframework运行环境,所以我就不能使用c#来写了。这就让我很为难了。
在网上我也看了好多关于vb操作xml的的信息,但是看不太明白。或者说,基本没看懂……所以,请各位大神相助。先在此谢过! 展开
2个回答
展开全部
我这里有个vb xml类,你试着用用!你需要了解dom 和xpath方面的知识。
下面的代码需要在工程中添加引用: microsoft xml 6.0
'*********************************************************************************************
'mornxml.cls xml解析类
'AUTHOR Morn Woo
'完成时间 2011年4月18日
'版本 ver1.0 20110111,修正了部分错误
'*********************************************************************************************
Option Explicit
Option Compare Text
DefInt I
DefStr S
DefDate D
DefLng L
DefBool B
Dim mvarsXmlFile As String 'xmlFile属性内存变量
Dim mvarsXmlContent As String 'xmlContent属性内存变量
Dim mvarsXmlRoot As MSXML2.DOMDocument 'xmlRoot属性内存变量
Public Property Let XmlRoot(ByRef vData As MSXML2.DOMDocument)
Set mvarsXmlRoot = vData
End Property
Public Property Get XmlRoot() As MSXML2.DOMDocument
Set XmlRoot = mvarsXmlRoot
End Property
Public Property Let XmlFile(ByVal vData As String)
mvarsXmlFile = vData
End Property
Public Property Get XmlFile() As String
XmlFile = mvarsXmlFile
End Property
Public Property Let XmlContent(ByVal vData As String)
mvarsXmlContent = vData
End Property
Public Property Get XmlContent() As String
XmlContent = mvarsXmlContent
End Property
'类初始化
Private Sub Class_Initialize()
Me.XmlContent = ""
Me.XmlFile = ""
'Me.XmlRoot = New MSXML2.DOMDocument
Set mvarsXmlRoot = New MSXML2.DOMDocument
End Sub
'Private Sub Class_Terminate()
' XmlRoot.abort
'
'End Sub
Function fun_XmlLoad(ByVal sFilePath As String) As Boolean
On Error GoTo Morn
fun_XmlLoad = False
If Dir(sFilePath, vbNormal) = "" Then Exit Function
Me.XmlRoot.Load sFilePath
Me.XmlFile = Trim(sFilePath)
Do While XmlRoot.readyState <> 4
DoEvents
Loop
Me.XmlFile = XmlRoot.XML
fun_XmlLoad = True
Exit Function
Morn:
End Function
'找到节点对象
Function fun_GetElement(ByVal sItem As String) As MSXML2.IXMLDOMElement
'Set fun_GetElement = New ms
On Error GoTo Morn
Set fun_GetElement = Me.XmlRoot.selectSingleNode(sItem)
Exit Function
Morn:
End Function
'读取节点text函数
Function fun_GetElementText(ByVal sItem As String) As String
Dim oElement As MSXML2.IXMLDOMElement
fun_GetElementText = ""
On Error GoTo Morn
Set oElement = Me.XmlRoot.selectSingleNode(sItem)
If oElement Is Nothing Then Exit Function
fun_GetElementText = oElement.Text
Exit Function
Morn:
End Function
'读取节点属性值函数,默认读取value属性值
Function fun_GetProperty(ByVal sItem As String, Optional ByVal sPropertyName = "value") As String
Dim oElement As MSXML2.IXMLDOMElement
fun_GetProperty = ""
On Error GoTo Morn
Set oElement = Me.XmlRoot.selectSingleNode(sItem)
Set oElement = Me.XmlRoot.selectSingleNode(sItem)
If oElement Is Nothing Then Exit Function
fun_GetProperty = oElement.getAttribute(sPropertyName)
Exit Function
Morn:
End Function
下面的代码需要在工程中添加引用: microsoft xml 6.0
'*********************************************************************************************
'mornxml.cls xml解析类
'AUTHOR Morn Woo
'完成时间 2011年4月18日
'版本 ver1.0 20110111,修正了部分错误
'*********************************************************************************************
Option Explicit
Option Compare Text
DefInt I
DefStr S
DefDate D
DefLng L
DefBool B
Dim mvarsXmlFile As String 'xmlFile属性内存变量
Dim mvarsXmlContent As String 'xmlContent属性内存变量
Dim mvarsXmlRoot As MSXML2.DOMDocument 'xmlRoot属性内存变量
Public Property Let XmlRoot(ByRef vData As MSXML2.DOMDocument)
Set mvarsXmlRoot = vData
End Property
Public Property Get XmlRoot() As MSXML2.DOMDocument
Set XmlRoot = mvarsXmlRoot
End Property
Public Property Let XmlFile(ByVal vData As String)
mvarsXmlFile = vData
End Property
Public Property Get XmlFile() As String
XmlFile = mvarsXmlFile
End Property
Public Property Let XmlContent(ByVal vData As String)
mvarsXmlContent = vData
End Property
Public Property Get XmlContent() As String
XmlContent = mvarsXmlContent
End Property
'类初始化
Private Sub Class_Initialize()
Me.XmlContent = ""
Me.XmlFile = ""
'Me.XmlRoot = New MSXML2.DOMDocument
Set mvarsXmlRoot = New MSXML2.DOMDocument
End Sub
'Private Sub Class_Terminate()
' XmlRoot.abort
'
'End Sub
Function fun_XmlLoad(ByVal sFilePath As String) As Boolean
On Error GoTo Morn
fun_XmlLoad = False
If Dir(sFilePath, vbNormal) = "" Then Exit Function
Me.XmlRoot.Load sFilePath
Me.XmlFile = Trim(sFilePath)
Do While XmlRoot.readyState <> 4
DoEvents
Loop
Me.XmlFile = XmlRoot.XML
fun_XmlLoad = True
Exit Function
Morn:
End Function
'找到节点对象
Function fun_GetElement(ByVal sItem As String) As MSXML2.IXMLDOMElement
'Set fun_GetElement = New ms
On Error GoTo Morn
Set fun_GetElement = Me.XmlRoot.selectSingleNode(sItem)
Exit Function
Morn:
End Function
'读取节点text函数
Function fun_GetElementText(ByVal sItem As String) As String
Dim oElement As MSXML2.IXMLDOMElement
fun_GetElementText = ""
On Error GoTo Morn
Set oElement = Me.XmlRoot.selectSingleNode(sItem)
If oElement Is Nothing Then Exit Function
fun_GetElementText = oElement.Text
Exit Function
Morn:
End Function
'读取节点属性值函数,默认读取value属性值
Function fun_GetProperty(ByVal sItem As String, Optional ByVal sPropertyName = "value") As String
Dim oElement As MSXML2.IXMLDOMElement
fun_GetProperty = ""
On Error GoTo Morn
Set oElement = Me.XmlRoot.selectSingleNode(sItem)
Set oElement = Me.XmlRoot.selectSingleNode(sItem)
If oElement Is Nothing Then Exit Function
fun_GetProperty = oElement.getAttribute(sPropertyName)
Exit Function
Morn:
End Function
追问
能不能给我写一个很简单的实例,譬如:
1、创建对象
2、创建根节点
3、寻找所需要的节点
4.、修改节点
就这么几句的代码就好。
我好笨的,也没学过VB,这里实在是看不出来什么。这样的代码我也看了好多了 不知道怎么组合起来使用……
追答
下面的代码需要在工程中添加引用: microsoft xml 6.0
Private Sub s_MakeFomartExample(sPath As String)
Screen.MousePointer = vbHourglass
Dim oXml As New MSXML2.DOMDocument '主xml文档:流程情况
Dim oRoot As MSXML2.IXMLDOMElement
Dim oElement As MSXML2.IXMLDOMElement
Dim oNowElement As MSXML2.IXMLDOMElement
Dim oNodeTmp As MSXML2.IXMLDOMNode
'1 建立 xml文件
oXml.async = "false"
'1.1 版本信息
oXml.appendChild oXml.createProcessingInstruction("xml", "version='1.0' encoding='GBK'")
'1.2 创建根节点
Set oXml.documentElement = oXml.createElement("data-File") '
Set oRoot = oXml.documentElement
Set oElement = oXml.createElement("work")
oElement.Text = "属性workid 是事项id,包含的办件的辨识信息,即相同的办件信息不管在何时被用来生成数据文件" & _
"都应该生成相同的数据文件ID,这样导入程序可以根据此Id判断数据是否已经被导入!" & _
"避免重复导入! "
oElement.setAttribute "workid", "第一个办件的workid"
oRoot.appendChild oElement '起始节点
Set oNowElement = oElement
Set oElement = oXml.createElement("transname")
oElement.Text = "第一个办件的事项名称" '导入时需要转换为行政审批系统的事项id和名称。
oNowElement.appendChild oElement
Set oElement = oXml.createElement("depname")
oElement.Text = "部门名称"
oNowElement.appendChild oElement
oXml.save sPath & "\test.xml" '保存xml文件
Screen.MousePointer = vbDefault
MsgBox "写格式示例文件:『" & sPath & "\test.xml" & "』成功!"
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询