Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.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
Xml 搜索对象是否包含来自其他数组的值_Xml_Powershell - Fatal编程技术网

Xml 搜索对象是否包含来自其他数组的值

Xml 搜索对象是否包含来自其他数组的值,xml,powershell,Xml,Powershell,我有一个如下所示的XML文件: <?xml version="1.0" encoding="UTF-8"?> <catalog> <software> <name>MozillaFirefox</name> <version>31.3.0</version> <installer_location>/Mozilla/Firefox/31.3.0.exe</instal

我有一个如下所示的XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <software>
    <name>MozillaFirefox</name>
    <version>31.3.0</version>
    <installer_location>/Mozilla/Firefox/31.3.0.exe</installer_location
  </software>
  <software>
    <name>GoogleChrome</name>
    <version>35.7</version>
    <installer_location>/Google/Chrome/35.7.msi</installer_location
  </software>
  <software>
    <name>MozillaFirefox</name>
    <version>33.4.0</version>
    <installer_location>/Mozilla/Firefox/33.4.0.exe</installer_location>
  </software>
</catalog>
哪个输出:

name             version      installer_location
----             -------      ------------------ 
MozillaFirefox   31.3.0       /Mozilla/Firefox/31.3.0.exe
GoogleChrome     35.7         /Google/Chrome/35.7.msi

我应该编写什么代码来搜索$softwareToBeInstalled中的所有名称,将名称与数组$softwareList中包含的名称进行比较,并写入$softwareList中包含但不在$softwareToBeInstalled中的任何软件名称(在我的示例中为Arduino)到另一个变量$missingSoftware?

我将循环检查
$softwareToBeInstalled
中的名称,并为每个名称检查名称是否包含在数组
$softwareList
中。我可能会这样做:

$desiredResults = @()
$softwareToBeInstalled.name.foreach({ 
     if(!$softwareList.contains($_)){$desiredResults += $_})
})
请注意,根据$softwareToBeInstalled对象的类型,您可能需要调用枚举器才能调用.foreach。您可能还可以使用:

$softwareToBeInstalled.foreach

使用与上面相同的代码,但引用dollarsign下划线点名称而不是dollarsign下划线。

创建一个可调整大小的集合,然后检查每个元素是否出现在第二个列表中(如果没有,则添加):


由于某些原因,我不得不在最后一句中拼写dollarsign下划线,否则它会自动显示斜体,并截断下划线。
Compare Object$softwareList$softwareToBeInstalled.name
$softwareToBeInstalled.foreach
# items can be added to a collection
# this method creates an empty array and pass it to a collection
$missingSoftware = {""}.Invoke() 


Foreach($name in $softwareList){
# if doesnt appear
if (-not($softwareToBeInstalled.name.Contains($name))){
        # add it to the collection
        $missingSoftware.add($name)
    }
}

write $missingSoftware