如何验证节点值并根据条件记录值?

时间:2012-10-30 13:15:08

标签: xml powershell powershell-v2.0

我正在使用具有以下结构的xml文件。

 <NodeData>
      <XPath>//VersionInfo/VersionList[Name = "FileCopy"]</XPath>
      <Node>
        <VersionList xmlns="">
          <Name>Core_FileCopy</Name>
          <Version>10.21.1.3</Version>
        </VersionList>
      </Node>
    </NodeData>

我正在使用值版本和名称并将它们存储在日志文件中。

    $FileList= Get-Childitem -path "Z:\XmlFiles" -Include "*.xml" -recurse | ?{$_.GetType() -ne [System.IO.DirectoryInfo]} 
    $LikeArray = @("10.21.*","10.20.*","10.11.*")

    Foreach ( $File in $Filelist ) { 

        "Processing file $File.FullName"
        $xmldata= [xml] (Get-content $File.FullName);  
        $VersionLists = $xmldata.SelectNodes("//VersionList")    # Get the versionlist node

        # If it has VersionList node
        If ( $VersionLists -ne $null) {

            Foreach ($VersionList in $VersionLists) { 
$VersionList | Select Version | Where-Object {$_ -notlike $LikeArray }
            }

        }
    }

但是,只有在没有版本号

时才需要存储名称和版本
  

( “10.21。”, “10.20。”,“10.11。*)。

我已经修改了我的条件,但仍显示所有版本号

1 个答案:

答案 0 :(得分:0)

看看这是否符合您的要求。

$FileList= Get-Childitem -path "Z:\XmlFiles" -Include "*.xml" -recurse | ?{$_.GetType() -ne [System.IO.DirectoryInfo]} 
$LikeArray = @("10.21","10.20","10.11")
Foreach ( $File in $Filelist ) { 
        "Processing file $File.FullName"
        $xmldata= [xml] (Get-content $File.FullName);  
        $VersionLists = $xmldata.SelectNodes("//VersionList")    # Get the versionlist node
        Foreach ($VersionList in $VersionLists) { 
         if ( $LikeArray -notcontains $VersionList.Version.substring(0,5)) {
                    $VersionList | select Name, Version 
               }
        }
}
相关问题