我可以帮助使用PowerShell将节点添加到现有XML中吗?
这就是我所拥有的:
<agentList>
<newAgent>
<name>Justice, Kari</name>
<mu>4690</mu>
<agentData>
<group>
<description>GROUP_DESCRIPTION</description><value>GROUP_NAME</value>
</group>
</agentData>
</newAgent>
</agentList>
我需要添加:
<group><description>ACSR System Logon</description><value></value></group>
<group><description>Current Call Type</description><value></value></group>
<group><description>OMS</description><value></value></group>
<group><description>RIO Log-in</description><value></value></group>
<group><description>Site</description><value></value></group>
下面:
<agentList>
<newAgent>
<name>Justice, Kari</name>
<mu>4690</mu>
<agentData>
<group>
<description>GROUP_DESCRIPTION</description><value>GROUP_NAME</value>
<====== HERE
<====== HERE
<====== HERE
<====== HERE
</group>
</agentData>
</newAgent>
</agentList>
我可能在XML上有多个用户,所以我想要使用FOREACH行...但是我在powershell中使用xml失去了...如果有人可以分享一些想法,我将很乐意使用它。 ..
答案 0 :(得分:5)
应该是这样的:
$GroupList = @{"Mickey" = "mouse";"Minnie" = "mouse";"Goofy" = "dog"}
$xml=[xml](get-content .\yourfile.xml)
$xml | Select-Xml -XPath '/agentList/newAgent/agentData' | foreach-object{$_.node.removeall()} #clear group section
$groupNode = $xml.createelement("group")
foreach ($description in $($GroupList.keys))
{
$descNode = $xml.createelement("description")
$descNode.setattribute("value",$description)
$groupNode.appendchild($descNode)
$valueNode = $xml.createelement("value")
$valueNode.setattribute("value",$GroupList[$description])
$groupNode.appendchild($valueNode)
}
$xml.selectsinglenode("agentList/newAgent/agentData").appendchild($groupNode)
$xml.save("C:\YourPathHere\test.xml")
**此代码假定“group”元素已存在于“。\ yourfile.xml”中。