我已经解压缩了XML文件的默认安装,该文件预先装有XML设置,例如...
<system.web>
<compilation targetFramework="4.6.1" debug="false" />
<httpRuntime targetFramework="4.6.1" requestPathInvalidCharacters=""/>
</system.web>
我在每个发行版上都会进行一系列环境更改,因此它在system.web部分增加了一行。
<system.web>
<compilation targetFramework="4.6.1" debug="false" />
<httpRuntime targetFramework="4.6.1" requestPathInvalidCharacters=""/>
<customErrors mode="Off" />
</system.web>
我将在Octopus部署中使用它,我相信它以Powershell 2.0为基础。
在Powershell脚本中,如何添加...
customErrors mode =“Off” /
...声明到该XML文件的现有system.web部分?
提前致谢
(先前链接的重复问题不是一个简洁的答案-谢谢)
最佳答案
经过大量的反复试验,发现它...
我之前的XML ...
<?xml version="1.0"?>
<configuration>
<anothernode>
<notalot target="1" debug="dish" />
</anothernode>
<system.web>
<compilation targetFramework="4.6.1" debug="false" />
<httpRuntime targetFramework="4.6.1" requestPathInvalidCharacters="" />
</system.web>
</configuration>
Powershell代码,我选择改用$ var值,但是没有什么阻止您将硬编码的东西放入代码中,但请记住它们周围的''引号!
# This will add <customErrors mode="Off" /> to the system.web section
$filename = "C:\Users\user\Desktop\Neil\newxmlsettings.xml"
$var1 = 'customErrors'
$var2 = 'mode'
$var3 = 'Off'
#
[xml]$xml = Get-Content $filename
$body = [xml]$xml
$newnode = $body.CreateElement($var1)
$newnode.SetAttribute($var2,$var3)
$body.SelectSingleNode("//system.web").AppendChild($newnode)
$xml.Save($filename)
仅需要AppendChild语句中带引号的“system.web”,因为它有一个。其中,对于单元素名称,您可以省略其双引号。
最终XML ...
<?xml version="1.0"?>
<configuration>
<anothernode>
<notalot target="1" debug="dish" />
</anothernode>
<system.web>
<compilation targetFramework="4.6.1" debug="false" />
<httpRuntime targetFramework="4.6.1" requestPathInvalidCharacters="" />
<customErrors mode="Off" />
</system.web>
</configuration>
关于xml - Powershell-XML在现有部分中插入新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47527385/