【发布时间】:2018-12-07 09:58:42
【问题描述】:
想要在 system.web 元素中添加另一个子元素。 到目前为止,我已经尝试了多种方法,但是,没有奏效。 我已经创建了这段代码,我不确定它是否接近。
到目前为止的代码:
#setting variables
$path = "H:\PSTesting\Logs\ExerciseXml.xml"
$xml = Get-Content $path
#Creating element
$child = $xml.CreateElement("Test")
#setting attributes
$child.SetAttribute('Testing','hey = "something"')
#adding attributes to the location
$xml.'system.web'.AppendChild($child)
#save file
$xml.Save($path)
下面是我需要更改的 XML。 当前:
<?xml version="1.0" encoding="UTF-8"?>
-<configuration>
-<system.web>
<authentication mode="None"/>
<compilation targetFramework="4.5.1" debug="false"/>
<httpRuntime targetFramework="4.5.1"/>
</system.web>
</configuration>
以下是运行代码的预期结果。
<?xml version="1.0" encoding="UTF-8"?>
-<configuration>
-<system.web>
<authentication mode="None"/>
<compilation targetFramework="4.5.1" debug="false"/>
<httpRuntime targetFramework="4.5.1"/>
<Testing Hey = 'something'>
</system.web>
</configuration>
任何帮助将不胜感激。 提前致谢!
【问题讨论】:
标签: xml powershell scripting