我需要替换“XXXXXX”和“YYYY”的值。引号中的替换将始终相同。但引号中的原始值总是不同的。我对编码很陌生,所以任何帮助都会受到赞赏。
<?xml version="1.0" encoding="utf-8"?>
<CellProgram>
<CellConfiguration CellName="XXXXXX" PartName="YYYYY">
答案 0 :(得分:1)
您需要添加对 Microsoft XML 库的引用才能使用XML DomDocument。
使用此库,您需要执行以下步骤。
LoadXML
方法从字符串加载它。)这就是我想出来的。
Public Sub test()
Dim doc As New DOMDocument
Const filePath As String = "C:\Users\x1ucjk2\Documents\test.xml"
Dim isLoaded As Boolean
isLoaded = doc.Load(filePath)
If isLoaded Then
Dim CellConfigurationList As MSXML2.IXMLDOMNodeList
Set CellConfigurationList = doc.getElementsByTagName("CellConfiguration")
Dim attr As MSXML2.IXMLDOMAttribute
Dim node As MSXML2.IXMLDOMElement
For Each node In CellConfigurationList
For Each attr In node.Attributes
If attr.Name = "CellName" Then
attr.Value = "Test"
ElseIf attr.Name = "PartName" Then
attr.Value = "AuxCell"
End If
Next attr
Next node
doc.Save filePath
End If
End Sub