我很难想到如何在XML文件中找到特定值并进行修改的方法。所以,我开始使用谷歌搜索并找到一篇文章。但是,我似乎无法理解用作解决问题的代码。我也尝试修改代码,但不幸的是我无法使其工作。
<Configuration>
<Add SourcePath="\\sample" ApplicationEdition="32">
<Product ID="SampleProductID">
<Language ID="en-us" />
<Language ID="en-us" />
</Product>
我需要修改此值的内容<Add SourcePath=
我已多次尝试过,并且没有运气写vbscript来改变其值。我会感激任何帮助和提前感谢。
答案 0 :(得分:3)
Set x = Nothing
just for fun 演示:
Option Explicit
Dim oXDoc : Set oXDoc = CreateObject( "Msxml2.DOMDocument" )
oXDoc.setProperty "SelectionLanguage", "XPath"
oXDoc.async = False
oXDoc.load "..\data\31677574.xml"
If 0 = oXDoc.ParseError Then
WScript.Echo oXDoc.documentElement.xml
Dim sXPath : sXPath = "/Configuration/Add"
Dim ndFnd : Set ndFnd = oXDoc.selectSingleNode(sXPath)
If Not ndFnd Is Nothing Then
ndFnd.getAttributeNode("SourcePath").value = "SomeOtherValue"
WScript.Echo "----------------", vbCrLf & oXDoc.documentElement.xml
Else
WScript.Echo sXPath, "- not found"
End If
Else
WScript.Echo oXDoc.ParseError.Reason
End If
输出:
cscript 31677574-2.vbs
<Configuration>
<Add SourcePath="\\sample" ApplicationEdition="32">
<Product ID="SampleProductID">
<Language ID="en-us"/>
<Language ID="en-us"/>
</Product>
</Add>
</Configuration>
----------------
<Configuration>
<Add SourcePath="SomeOtherValue" ApplicationEdition="32">
<Product ID="SampleProductID">
<Language ID="en-us"/>
<Language ID="en-us"/>
</Product>
</Add>
</Configuration>
答案 1 :(得分:0)
试试此代码
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "False"
xmlDoc.Load("T.xml")
Set objAdd = xmlDoc.selectSingleNode("//Configuration/Add")
objAdd.Attributes.getNamedItem("SourcePath").Text = "Test"
xmlDoc.Save "T.xml"
Set objAdd = Nothing
Set xmlDoc = Nothing
我建议您查看这些页面