powershell将xml对象转换为字符串

时间:2015-05-13 20:49:43

标签: powershell xml-parsing

我试图获取xml的值来创建目录。在第一行中,它将返回一个仅包含内部xml的表。但是在foreach循环中,它为$ loanNumber返回@ {$ _.Value.InnerXML = 00000027627-00001}。我怎样才能将其转换为数字?有没有办法可以将它转换为字符串然后拆分=符号?我一直在尝试这种方式并且没有成功。有没有更好的方法来获取价值而不是之前的垃圾?

cls

[xml]$file = Get-Content "F:\Peter Test\index.xml"
$hostdirectory = "F:\PShell Testing"

$file.ExportedResult.Docs.ExportedDoc.Doc.UdiValues.UdiValue | ? {$_.Name -eq "Loan Number"} | Select-Object {$_.Value.InnerXML} -Unique | Format-Table


 foreach($loanNumber in $file.ExportedResult.Docs.ExportedDoc.Doc.UdiValues.UdiValue | ? {$_.Name -eq "Loan Number"} | Select-Object {$_.Value.InnerXML} -Unique){
 Write-Host "hey"


    Write-Host $loanNumber
    $hostdirectoryPlusLoan =  $hostdirectory + "\" + $loanNumber
    if(!(test-path $hostdirectoryPlusLoan)){
        New-Item $hostdirectoryPlusLoan -ItemType directory
    }

 }

1 个答案:

答案 0 :(得分:0)

在Select-Object命令中插入-ExpandProperty

Select-Object -ExpandProperty {$_.Value.InnerXML} -Unique

在这种情况下,它应该只返回“loannumber”字符串,“00000027627-00001”。

你也可以这样做:

  $hostdirectoryPlusLoan =  "$hostdirectory\$loanNumber"

而不是

  $hostdirectoryPlusLoan =  $hostdirectory + "\" + $loanNumber

编辑:

你至少使用两次:     $ file.ExportedResult.Docs.ExportedDoc.Doc.UdiValues.UdiValue | ? {$ .Name -eq“贷款号码”} | Select-Object {$ .Value.InnerXML} -Unique

那么说什么

# use Select-Object <propertyName> -ExpandProperty -Unique twice to dig down into the nesting
# also, use "Where-Object" for "?"
# and mostly using a variable to hold the list of loan numbers so you don't have to filter and select exactly the same way twice.
$LoanNumberList = $file.ExportedResult.Docs.ExportedDoc.Doc.UdiValues.UdiValue | Where-Object {$_.Name -eq "Loan Number"} | Select-Object Value -ExpandProperty -Unique | Select-Object InnerXML -ExpandProperty -Unique

# assuming the above works, you can then use $LoanNumberList collection at will:

$LoanNumberList
foreach ($LoanNumber in $LoanNumberList) {
    # do stuff
    }