xml - 无法替换PowerShell中的字符串

标签 xml string powershell powershell-2.0

            if ("$string" -match "$key")
            {
                    Write-Host "Attempting to replace $string with $value in $sourcefilename"

                   (Get-Content $sourcefilename).Replace("{{$string}}",$value) | set-content $destinationfilename

            }
          }
有人可以告诉我如何用字符串替换各个值。

最佳答案

下次您提问题时,请确定您的问题。
您要替换字典的键值对中的文本。

您的错误是,您总是阅读$sourcefilename,替换了文本并写入了destinationfilename。可能只有字典的最后一个条目被替换了。

因此,您只需阅读一次内容,然后遍历字典并替换值:

$templatecontent = Get-Content $sourcefilename
$Dictionary.Keys | % { $templatecontent = $templatecontent -replace "{{$_}}", ($Dictionary[$_]) } 
templatecontent | set-content $destinationfilename

关于xml - 无法替换PowerShell中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36973464/

相关文章:

c# - XDocument.Save() 在每个 XElement 上添加不需要的命名空间

java - JAXB:如何获取包装类型的值(value)?

python - python3 中的 byte 与 str.encode

python - 在python中随机播放字符串

用于从文件夹列表中省略遍历访问被拒绝的文件夹的 Powershell 脚本

.net - 如何使用 PowerShell 查找 Active Directory 类的 "Schema-Id-Guid"和属性的 "System-Id-Guid"

xml - 多大的 RSS 提要 XML 文件太大了?

c# - 处理 XML 更改的好方法

c - 确定字符串是否是 C 中的有效 IPv4 地址

powershell - 在参数中使用点源函数