You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell如何定位含点号的XPath节点并修改.NET配置文件值

问题原因

  • 你当前的XPath仅定位到了<setting>节点,而你需要修改的文本内容存放在<setting>的子节点<value>内,而非<setting>节点的属性,直接对$node.value赋值会因属性不存在报错
  • SelectNodes()返回的是节点集合对象,即便只匹配到一个节点,直接操作集合的自定义属性也会触发异常,定位单个节点优先使用SelectSingleNode()

解决方法

你已经正确处理了带点号节点的匹配逻辑,只需要调整XPath直接定位到<value>节点,再修改它的InnerText属性即可:

# 定位DBConString对应的value节点
$node = $xml.SelectSingleNode("configuration/userSettings/*[local-name()='DatabaseUpdate.Properties.Settings']/setting[@name='DBConString']/value")
$node.InnerText = "$(EzAcquireConStr)"

# 定位ScriptPath对应的value节点
$node = $xml.SelectSingleNode("configuration/userSettings/*[local-name()='DatabaseUpdate.Properties.Settings']/setting[@name='ScriptPath']/value")
$node.InnerText = "$(System.DefaultWorkingDirectory)/extracted/DBScripts/app/"

修改后完整脚本

- powershell: |
    dir
    Write-Host "Verify DB Connection String is Accurate"
    if("$(EzAcquireConStr)" -like '*risk*') {
          # Write-Host 'Your string contains the word world'
    } else {
          Write-Host 'Default Connection string does not contain word risk, failing pipeline'
          exit 1
    }
    Write-Host "Manipulate Config to match repo's DBScripts"
    $xmlFileName = "$(System.DefaultWorkingDirectory)/DatabaseUpdate/DatabaseUpdate.exe.config"
    $xml=New-Object XML
    $xml.Load($xmlFileName)
    # 修正后的节点定位和赋值逻辑
    $node = $xml.SelectSingleNode("configuration/userSettings/*[local-name()='DatabaseUpdate.Properties.Settings']/setting[@name='DBConString']/value")
    $node.InnerText = "$(EzAcquireConStr)"
    $node = $xml.SelectSingleNode("configuration/userSettings/*[local-name()='DatabaseUpdate.Properties.Settings']/setting[@name='ScriptPath']/value")
    $node.InnerText = "$(System.DefaultWorkingDirectory)/extracted/DBScripts/app/"
    $xml.Save($xmlFileName)
    Write-Host "Run DB Update Using above criteria"
    & $(System.DefaultWorkingDirectory)/DatabaseUpdate/DatabaseUpdate.exe
    Write-Host "If DB Update Errors, fail pipeline"
    if ($lastexitcode -ne 0) {
      exit $LASTEXITCODE
    }
  displayName: 'Inject DBScripts Path for Tool'

内容的提问来源于stack exchange,提问作者John Casillas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 21:27:03