在 Powershell 中解析 XML 文件
Parse XML file in Powershell
我正在尝试从 Yahoo 检索汇率
$browser = New-Object System.Net.WebClient
$browser.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials
$url = 'http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote'
$xml = New-Object xml
$xml.Load($url)
[xml]$YahooDataDocument = $xml.InnerXml
$YahooDataDocument.List.resources.resource | format-table name,price
但是我无法按要求获取数据,我只得到:
Name price
---- -----
resource
resource
resource
resource
resource
.
.
..
试试这个:
$browser = New-Object System.Net.WebClient
$browser.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials
$url = 'http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote'
[xml]$xml = $browser.DownloadString($url)
$resultset = New-Object System.Collections.ArrayList
foreach ($quote in $xml.list.resources.resource){
$singlequote = New-Object System.Collections.Specialized.OrderedDictionary
foreach($element in $quote.field){
$singlequote.Add($element.name,$element.'#text')
}
$resultset += New-Object PSObject -Property $singlequote
}
$resultset | format-table
事情是当你得到 $xml.list.resources.resource 时它变成 System.Array 所以你需要处理每个对象分开。
我正在尝试从 Yahoo 检索汇率
$browser = New-Object System.Net.WebClient
$browser.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials
$url = 'http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote'
$xml = New-Object xml
$xml.Load($url)
[xml]$YahooDataDocument = $xml.InnerXml
$YahooDataDocument.List.resources.resource | format-table name,price
但是我无法按要求获取数据,我只得到:
Name price ---- ----- resource resource resource resource resource . . ..
试试这个:
$browser = New-Object System.Net.WebClient
$browser.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials
$url = 'http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote'
[xml]$xml = $browser.DownloadString($url)
$resultset = New-Object System.Collections.ArrayList
foreach ($quote in $xml.list.resources.resource){
$singlequote = New-Object System.Collections.Specialized.OrderedDictionary
foreach($element in $quote.field){
$singlequote.Add($element.name,$element.'#text')
}
$resultset += New-Object PSObject -Property $singlequote
}
$resultset | format-table
事情是当你得到 $xml.list.resources.resource 时它变成 System.Array 所以你需要处理每个对象分开。