本文介绍了通过PowerShell将数据追加到现有的xml文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下是我所拥有的:
$ b $可以帮助您使用powershell将节点添加到现有的XML中b

 < agentList> 
< newAgent>
< name> Justice,Kari< / name>
mu 4690< mu>
< agentData>
< group>
< description> GROUP_DESCRIPTION< / description><值> GROUP_NAME< /值>
< / group>
< / agentData>
< / newAgent>
< / agentList>

我需要添加这个:

<$ < group>< description> ACSR系统登录< / description><值>< /值>< / group>
< group>< description>当前呼叫类型< / description><值>< /值>< / group>
< group>< description> OMS< / description><值>< /值>< / group>
< group>< description> RIO登录< / description><值>< /值>< / group>
< group>< description>网站< / description><值>< /值>< / group>

这里:

 < agentList> 
< newAgent>
< name> Justice,Kari< / name>
mu 4690< mu>
< agentData>
< group>
< description> GROUP_DESCRIPTION< / description><值> GROUP_NAME< /值>
< ====== HERE
< ====== HERE
< ====== HERE
< ===== = HERE
< / group>
< / agentData>
< / newAgent>
< / agentList>

我可能在XML上有多个用户,因此我正在考虑使用FOREACH行。但我还是有点在PowerShell中使用xml丢失...如果任何人都可以分享一些想法,我会很高兴地与它玩... ...

它应该是这样的:

pre $ 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 .keys))
{
$ descNode = $ xml.createelement(description)
$ descNode.setattribute(value,$ description)
$ groupNode.appendchild $ descNode)

$ valueNode = $ xml.createelement(value)
$ valueNode.setattribute(value,$ GroupList [$ description])
$ groupNode。 appendchild($ valueNode)

$ b $ xml.selectsinglenode(agentList / newAgent / agentData)。appendchild($ groupNode)
$ xml.save(C:\\ \\ YouourPathHere \test.xml)

**此代码假设group元素为已存在于.\yourfile.xml中。


Can I have some help to add nodes into an existing XML using powershell?

Here is what I have:

<agentList>
 <newAgent>
      <name>Justice, Kari</name>
      <mu>4690</mu>
  <agentData>
   <group>
       <description>GROUP_DESCRIPTION</description><value>GROUP_NAME</value>
   </group>
  </agentData>
 </newAgent>
</agentList>

and I need to add this:

  <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>

Here:

<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>

I may have more than one user on the XML so I was thinking to use the FOREACH line.. but im kinda lost using xml in powershell... If anyone can share some Idea I will be pleased to play with it...

解决方案

It should be something along the lines of this:

$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")

** This code assumes that the "group" element is already existing in ".\yourfile.xml".

这篇关于通过PowerShell将数据追加到现有的xml文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 20:43