使用Powershell动态读取选择的XML内容到Hashtable

时间:2013-08-25 00:56:39

标签: xml powershell hashtable

我正在编写执行以下内容的Powershell代码

  1. 读取目录中的XML文件的内容
  2. 对于目录中的每个XML文件 一个。打印XML文件的选定内容(不是所有节点) 湾将所选内容保存到名为$ securities
  3. 的哈希表中

    我无法做2(b)

    这是我的代码:

    $directory = 'C:\Scripts\Results'
    [hashtable]$securities = @{}
    $CUSIP=$NULL
    $Int_Rate=$NULL
    $Issue_Date=$NULL
    $Maturity_Date=$NULL
    
    foreach ($i in (Get-ChildItem $directory -Name)){
        write-host $i
        [xml]$xml = [xml](Get-Content $directory\$i)
        $CUSIP = $xml.AuctionData.AuctionAnnouncement.CUSIP
        Write-Host CUSIP $CUSIP 
        $Int_Rate = $xml.AuctionData.AuctionResults.InterestRate
        Write-Host Interest Rate $Int_Rate 
        $Issue_Date = $xml.AuctionData.AuctionAnnouncement.IssueDate
        Write-Host Issue Date $Issue_Date 
        $Maturity_Date = $xml.AuctionData.AuctionAnnouncement.MaturityDate
        Write-Host Maturity Date $Maturity_Date 
        Write-Host `n
        $securities.add=($i,$CUSIP,$Int_Rate,$Issue_Date,$Maturity_Date)
    
    }
    
    $securities
    

    这是我对美元证券的输出

    名称值
    ---- -----
    CUSIP {Jul_2012.xml,912828TE0,0.125,2012-07-31 ...}

    这不是我想要的价值。我正在寻找的价值就像是

    Jan_2011.xml CUSIP 912828PP9 利率1.125 发行日期2011-01-31 到期日2021-01-15

    Jan_2012.xml CUSIP 912828SA9 利率0.125 发行日期2012-01-31 到期日2022-01-15

    Jul_2010.xml CUSIP 912828NM8 利率1.250 发行日期2010-07-15 到期日2020-07-15

    Jul_2011.xml CUSIP 912828QV5 利率0.625 发行日期2011-07-29 到期日2021-07-15

    Jul_2012.xml CUSIP 912828TE0 利率0.125 发行日期2012-07-31 到期日2022-07-15

    请指出正确的方向。

    修改

    嗨大卫

    我试过

     $securities.add($i, [pscustomobject]@{'CUSIP'=$CUSIP;'Int_Rate'=$Int_Rate;'Issue_Date'=$Issue_Date;'Maturity_Date'=$Maturity_Date})
    

    并为$ securities获得以下输出

    Name Value                                                                                                 
    ----                           -----                                                                                                 
    Jul_2012.xml                   @{CUSIP=912828TE0; Int_Rate=0.125; Issue_Date=2012-07-31; Maturity_Date=2022-07-15}                   
    Jul_2011.xml                   @{CUSIP=912828QV5; Int_Rate=0.625; Issue_Date=2011-07-29; Maturity_Date=2021-07-15}                   
    Jul_2010.xml                   @{CUSIP=912828NM8; Int_Rate=1.250; Issue_Date=2010-07-15; Maturity_Date=2020-07-15}                   
    Jan_2012.xml                   @{CUSIP=912828SA9; Int_Rate=0.125; Issue_Date=2012-01-31; Maturity_Date=2022-01-15}                   
    Jan_2011.xml                   @{CUSIP=912828PP9; Int_Rate=1.125; Issue_Date=2011-01-31; Maturity_Date=2021-01-15}
    

    但我不知道如何引用它。

    例如,当我试图获得

    的价值时
    $securities.Jul_2012.xml('CUSIP')
    

    我收到错误:

    You cannot call a method on a null-valued expression.
    At C:\Scripts\treasury_holdings.ps1:24 char:1
    + $securities.Jul_2012.xml('CUSIP')
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull
    

    而不是

    912828TE0
    

    编辑:

    谢谢大卫。

    为了帮助他人,这是最新的代码:

    $directory = 'C:\Scripts\Results'
    [hashtable]$securities = @{}
    $CUSIP=$NULL
    $Int_Rate=$NULL
    $Issue_Date=$NULL
    $Maturity_Date=$NULL
    
    foreach ($i in (Get-ChildItem $directory -Name)){
        write-host $i
        [xml]$xml = [xml](Get-Content $directory\$i)
        $CUSIP = $xml.AuctionData.AuctionAnnouncement.CUSIP        
        $Int_Rate = $xml.AuctionData.AuctionResults.InterestRate     
        $Issue_Date = $xml.AuctionData.AuctionAnnouncement.IssueDate    
        $Maturity_Date = $xml.AuctionData.AuctionAnnouncement.MaturityDate            
        $securities.add($i, [pscustomobject]@{'CUSIP'=$CUSIP;'Int_Rate'=$Int_Rate;'Issue_Date'=$Issue_Date;'Maturity_Date'=$Maturity_Date})
        Write-Host CUSIP $securities[$i].CUSIP
        Write-Host Interest Rate $securities[$i].Int_Rate
        Write-Host Issue Date $securities[$i].Issue_Date
        Write-Host Maturity Date $securities[$i].Maturity_Date
        Write-Host `n
    }
    

1 个答案:

答案 0 :(得分:0)

问题在于您正在调用以下内容:

$securities.add=($i,$CUSIP,$Int_Rate,$Issue_Date,$Maturity_Date)

问题在于那里的等号。实际上做的是插入一个带有“add”键的记录,其余的值作为值。你真正想要的是用密钥$ i和其他所有东西的值添加记录。尝试创建其余值的[pscustomobject],并将它们添加到哈希表中,如下所示:

$securities.add($i, [pscustomobject]@{'CUSIP'=$CUSIP;'Int Rate'=$Int_Rate;'Issue Date'=$Issue_Date';'Maturity Date'=$Maturity_Date})

编辑: 尝试执行以下任一操作:

($securities.Jul_2012.xml).CUSIP

$securities['Jul_2012.xml'].CUSIP