对于Powershell来说,在我的远程工作中有一项非常复杂的任务。很难解释,但我会尝试。我有结构复杂的XML文件(该文件具有Visual Studio的设置):
<UserSettings>
<ToolsOptions>
<ToolsOptionsCategory RegisteredName="Environment" name="Environment">
</ToolsOptionsCategory>
</ToolsOptions>
</UserSettings>
任务添加到子节点下的“ ToolsOptionsCategory”部分:
<ToolsOptionsSubCategory PackageName="VS Setup Composition" RegisteredName="ProductUpdates" name="ProductUpdates">
<PropertyValue name="IsBackground">true</PropertyValue>
</ToolsOptionsSubCategory>
我尝试使用AppendChild和CreateElement,但是我不知道如何添加“ PackageName”和“ RegistereName”等属性。目标文件中没有该子节点,我需要添加它。如果您能帮助我并给我指路,我将很棒。
先感谢您!保持健康,待在家里:)
对于演示,我使用Here-String
[xml]$xml = @"
<UserSettings>
<ToolsOptions>
<ToolsOptionsCategory RegisteredName="Environment" name="Environment">
</ToolsOptionsCategory>
</ToolsOptions>
</UserSettings>
"@
在现实生活中,您将使用 [xml]$xml = Get-Content -Path 'D:\config.xml'
$node = $xml.UserSettings.ToolsOptions.ToolsOptionsCategory
# update the attributes for this node
$node.SetAttribute("PackageName", "VS Setup Composition")
$node.SetAttribute("RegisteredName", "ProductUpdates")
$node.SetAttribute("name", "ProductUpdates")
# create the new sub node (XmlElement)
$newNode = $xml.CreateElement("PropertyValue")
$newNode.SetAttribute("name", "IsBackground")
$newNode.InnerText = "true"
# add this new subnode to the 'ToolsOptionsCategory' node
$node.AppendChild($newNode)
$xml.Save('D:\newConfig.xml')
结果:
<UserSettings>
<ToolsOptions>
<ToolsOptionsCategory RegisteredName="ProductUpdates" name="ProductUpdates" PackageName="VS Setup Composition">
<PropertyValue name="IsBackground">true</PropertyValue>
</ToolsOptionsCategory>
</ToolsOptions>
</UserSettings>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句