我尝试更改我的 xml 文件中的 2 个内部文本,但是当我运行 save 命令时它只会保存其中一个更改。我不知道将两个内部文本保存回 xml 文件。
有人可以帮我吗?
这是我的代码:
Clear-Host
$Path = "C:\Program Files (x86)\Netcompany\GetOrganized Outlook Add-in 32-bit\Netcompany.Outlook.properties.config"
$XpathAlias = "/configuration/properties/configs/array/item/goAlias"
$XpathBaseUrl = "/configuration/properties/configs/array/item/baseUrl"
$CurrentAlias = Select-Xml -Path $Path -XPath $XpathAlias | Select-Object -ExpandProperty Node
$CurrentBaseUrl = Select-Xml -Path $Path -XPath $XpathBaseUrl | Select-Object -ExpandProperty Node
Write-Host $CurrentAlias.InnerText
Write-Host $CurrentBaseUrl.InnerText
Write-Host "Choose what GO Environment the Plugin should use:"
Write-Host
Write-Host " 0) Abort"
Write-Host " 1) Prod"
Write-Host
$validchoice = $false
while (-not $validchoice) {
$ChosenEnvironment = (Read-Host "Choose").Trim().ToUpperInvariant();
if ("0","1","2","3","4" -notcontains $ChosenEnvironment)
{
Write-Host -ForegroundColor Red "Invalid choice"
} else {
$validchoice = $true
}
}
Write-Host
$newsize = $null
switch ($ChosenEnvironment) {
"0" { Write-Host "Abort." }
"1" {
$CurrentBaseUrl.InnerText = "Prod";
$CurrentAlias.InnerText = "Prod";
$CurrentAlias.OwnerDocument.Save($Path);
$CurrentBaseUrl.OwnerDocument.Save($Path);
#Write-Host "GO miljø i Outlook plugin er sat til Prod miljøet"
}
}
答案 0 :(得分:0)
您正在加载和保存 xml 文件两次,每次保存只有一次更新,因为您已将 xml 加载到两个单独的变量中 >
$CurrentAlias = Select-Xml -Path $Path ..
$CurrentBaseUrl = Select-Xml ..
我会像这样简化代码:
$Path = "C:\Program Files (x86)\Netcompany\GetOrganized Outlook Add-in 32-bit\Netcompany.Outlook.properties.config"
# load the XML file only once
$xml = [System.Xml.XmlDocument]::new()
$xml.Load($Path)
接下来是执行 Read-Host
和验证所做的选择
最后,在有效的选择选项中
# set the new innerText properties (on the one and only xml you have loaded before)
$xml.SelectSingleNode("/configuration/properties/configs/array/item/goAlias").InnerText = "Prod"
$xml.SelectSingleNode("/configuration/properties/configs/array/item/baseUrl").InnerText = "Prod"
# save the xml
$xml.Save($Path)