Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell-混合2个XML文件_Xml_Powershell_Import_Export_Add - Fatal编程技术网

Powershell-混合2个XML文件

Powershell-混合2个XML文件,xml,powershell,import,export,add,Xml,Powershell,Import,Export,Add,我已经查看了现有主题,但没有发现任何内容。。 我有两个XML文件,除了一些行之外几乎相同。 其目的是从第一个XML中获取值,将其添加到第二个XML中,然后第二个XML将删除第一个XML文件 这是第一个文件的结构 <configuration > <protocol> <NATIVE> </NATIVE> <ICAP> </ICAP> <RPC> <ClientList> <

我已经查看了现有主题,但没有发现任何内容。。 我有两个XML文件,除了一些行之外几乎相同。 其目的是从第一个XML中获取值,将其添加到第二个XML中,然后第二个XML将删除第一个XML文件

这是第一个文件的结构

<configuration >
<protocol>
 <NATIVE>
 </NATIVE>
 <ICAP>
 </ICAP>
 <RPC>
  <ClientList>
    <items>
      <item value="X.X.X.X">
      <item value="A.A.A.A">
    </items>
  </ClientList>
</configuration>
但不是得到:

<ClientList>
    <items>
      <item value="X.X.X.X">
        <item value="A.A.A.A"/>
        <item value="B.B.B.B"/>
        <item value="C.C.C.C"/>
      </item>
    </items>
  </ClientList>

我得到:

<ClientList>
    <items>
      <item value="C.C.C.C">     <== Last entry read by PS...
        <item />
        <item />
        <item />

      </item>
    </items>
</ClientList>


这里有一种方法,您的文件包含无效的xml,请修复它们并尝试一下

$newXml = @"
<configuration>
<protocol>
<NATIVE></NATIVE>
<ICAP></ICAP>
 <RPC>
  <ClientList>
    <items>
        $(
            $xd.SelectNodes("//configuration/protocol/RPC/ClientList/items").InnerXml
            $xd2.SelectNodes("//configuration/protocol/RPC/ClientList/items").InnerXml
        )
    </items>
  </ClientList>
   </RPC>
   </protocol>
</configuration>
"@

$newXml | Out-File $xmlpath2
Remove-Item $xmlpath1 -Force
$newXml=@”
$(
$xd.SelectNodes(“//configuration/protocol/RPC/ClientList/items”).InnerXml
$xd2.SelectNodes(“//configuration/protocol/RPC/ClientList/items”).InnerXml
)
"@
$newXml |输出文件$xmlpath2
删除项$xmlpath1-强制

您可以试试这样的方法。它从
test1.xml
中的clientlist节点获取所有项目元素,并将它们添加到
test2.xml

$xml1 = [xml](Get-Content .\Desktop\test1.xml)
$xml2 = [xml](Get-Content .\Desktop\test2.xml)

#foreach item-nodes with value attribute in xml1
$xml1.SelectNodes("//configuration/protocol/RPC/ClientList/items/item[@value]") | % { 
    #check if item already exists. If not, add
    if($xml2.SelectSingleNode("//configuration/protocol/RPC/ClientList/items/item[@value='$($_.value)']") -eq $null) {
        $xml2.SelectSingleNode("//configuration/protocol/RPC/ClientList/items").AppendChild($xml2.CreateElement("item")).SetAttribute("value",$_.value)
    }
}

$xml2.Save("C:\Users\graimer\Desktop\test2.xml")
我已经在xml文件之前和之后添加了xml文件,因为我必须在它们成为有效的xml之前对它们进行修改(关闭一些元素)

test1.xml(之前)


test2.xml(之前)


test2.xml(之后)



一个良好的开端是为我们提供一致且有效的xml示例。结果中的项目与输入文件不匹配,且输入文件不完整。您好,谢谢您的回复并发现@“…”@。选择节点不选择任何内容,输出文件是空的,除了@“…”的内容。此外,这2个XML文件有114行,有很多设置。我的第一个想法是把它写进一个剧本中,而不是专门写那个剧本。我将深入研究SelectNode小程序/命令ThxWoha。我还有很多工作要做,才能确切地理解XML和PS是如何工作的。。。。你的剧本写得很好。条件是XML文件必须具有相同的大小(行数等)。非常感谢!我将处理这个NP;)如果你认为这个答案是正确的,请用复选标记来确认。我不知道你在说什么。上面的脚本不需要相同的文件大小、行数等。test1和test2可以不同,只要它们遵循相同的方案并且有效(所有元素都关闭)。它将只修改clientlist,并确保test1中的每个项目都是在test2中创建的(不创建重复项)。我将问题标记为已解决:)我说条件,因为我在这方面有错误。但一切都很好。再次感谢
$newXml = @"
<configuration>
<protocol>
<NATIVE></NATIVE>
<ICAP></ICAP>
 <RPC>
  <ClientList>
    <items>
        $(
            $xd.SelectNodes("//configuration/protocol/RPC/ClientList/items").InnerXml
            $xd2.SelectNodes("//configuration/protocol/RPC/ClientList/items").InnerXml
        )
    </items>
  </ClientList>
   </RPC>
   </protocol>
</configuration>
"@

$newXml | Out-File $xmlpath2
Remove-Item $xmlpath1 -Force
$xml1 = [xml](Get-Content .\Desktop\test1.xml)
$xml2 = [xml](Get-Content .\Desktop\test2.xml)

#foreach item-nodes with value attribute in xml1
$xml1.SelectNodes("//configuration/protocol/RPC/ClientList/items/item[@value]") | % { 
    #check if item already exists. If not, add
    if($xml2.SelectSingleNode("//configuration/protocol/RPC/ClientList/items/item[@value='$($_.value)']") -eq $null) {
        $xml2.SelectSingleNode("//configuration/protocol/RPC/ClientList/items").AppendChild($xml2.CreateElement("item")).SetAttribute("value",$_.value)
    }
}

$xml2.Save("C:\Users\graimer\Desktop\test2.xml")
<configuration>
<protocol>
 <NATIVE></NATIVE>
 <ICAP></ICAP>
 <RPC>
  <ClientList>
    <items>
      <item value="X.X.X.X" />
      <item value="A.A.A.A" />
    </items>
  </ClientList>
</RPC>
</protocol>
</configuration>
<configuration >
<protocol>
 <NATIVE>
 </NATIVE>
 <ICAP>
 </ICAP>
 <RPC>
  <ClientList>
    <items>
    </items>
  </ClientList>
</RPC>
</protocol>
</configuration>
<configuration>
  <protocol>
    <NATIVE>
    </NATIVE>
    <ICAP>
    </ICAP>
    <RPC>
      <ClientList>
        <items>
          <item value="X.X.X.X" />
          <item value="A.A.A.A" />
        </items>
      </ClientList>
    </RPC>
  </protocol>
</configuration>