从ArrayList中读取项目

时间:2015-10-09 04:38:43

标签: .net xml powershell xpath arraylist

我们得到了一个相同的XML,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Configuration>
  <Format_Version>1.0</Format_Version>
  <Reporting>
  </Reporting>
  <Jobs>
        <Job>
                <Name>SpaceCheck</Name>
                <Job_Type>SpaceCheck</Job_Type>
                <Schedule>
                    <Start_Time>0300</Start_Time>
                    <Frequency>24 hours</Frequency>
                    <Max_Run_Time_In_Seconds>10</Max_Run_Time_In_Seconds>
                </Schedule>
                <Parameters>
                    <Drive>C</Drive>
                    <Drive>D</Drive>
                </Parameters>
        </Job>
        <Job>
                <Name>CPUCheck</Name>
                <Job_Type>UsageMonitor</Job_Type>
                <Schedule>
                    <Frequency>3 minutes</Frequency>
                </Schedule>
                <Parameters>
                    <Threshold>90%</Threshold>
                    <Duration>10 minutes</Duration>
                </Parameters>
        </Job>
        <Job>
                <Name>overloaded CPUCheck</Name>
                <Job_Type>CPUcheck2</Job_Type>
                <Schedule>
                    <Frequency>3 minutes</Frequency>
                </Schedule>
                <Parameters>
                    <Threshold>80%</Threshold>
                    <Duration>50 minutes</Duration>
                </Parameters>
            </Job>
        <Job>
          <Name>Connection</Name>
          <Job_Type>Connectivity</Job_Type>
          <Schedule>
            <Start_Time>1900</Start_Time>
            <Frequency>1</Frequency>
            <Maximum_Runtime>30</Maximum_Runtime>
          </Schedule>
          <Parameters>
            <ToErrInHours>1</ToErrInHours>
            <Days>1</Days>
            <Threshold>70</Threshold>
          </Parameters>
        </Job>
      </Jobs>
    </Configuration>

这是我的代码,我试图存储在数组列表中并希望输出它,但由于某种原因 - 结果不是从数组列表中读取,请建议我是否需要直接从数组中获取项目ArrayList中。

# Loading the Config file into $xdoc
$xdoc = [xml] (Get-content "C:\Program Files\Lumina\NexTool\Tool.Configuration.xml")
$JobName = New-Object System.Collections.ArrayList

foreach ($item in $xdoc.Configuration.Jobs.job | % { $_.InnerText })
#| Where-Object {$_.Name }
{
$JobName.Add($item.SelectNodes("./Configuration/Jobs/Job/Name")) 

}

foreach ($item in $xdoc.SelectNodes("/Configuration/Jobs/Job/Name"))
{
Write-Host $item.InnerText
}

1 个答案:

答案 0 :(得分:0)

不确定为什么期望方法SelectNodes能够在$item上运行 - 它是一个字符串(来自$_.InnerText的结果),请检查以下代码:

foreach ($item in $xdoc.Configuration.Jobs.job | % { $_.InnerText })
#| Where-Object {$_.Name }
{
    $item | Get-Member -MemberType Method -Name Select*
    $item -is [String]
}

我建议改为使用Select-Xml cmdlet:

Select-Xml -Xml $xdoc -XPath //Job | 
    ForEach-Object { $null = $JobName.Add($_.Node.Name) }