Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Value from xml by PowerShell?

Tags:

powershell

xml

I have XML file with this data

<?xml version="1.0" encoding="windows-1251" ?>
<ValCurs Date="06/06/2012" name="Курби асъор">
 <Valute ID="036">
   <CharCode>AUD</CharCode> 
   <Nominal>1</Nominal> 
   <Name>Доллари Австралия</Name> 
   <Value>4,6430</Value> 
  </Valute>
 <Valute ID="944">
   <CharCode>AZN</CharCode> 
   <Nominal>1</Nominal> 
   <Name>Манати Озарбой&#1207;он</Name> 
   <Value>6,0677</Value> 
  </Valute>
 <Valute ID="826">
   <CharCode>GBP</CharCode> 
   <Nominal>1</Nominal> 
   <Name>Фунт-стерлинги Ингилистон</Name> 
   <Value>7,3156</Value> 
   </Valute>
...

and other

How can one get data from the Nominal and Value nodes by when Valute's attribute ID equals 826?

like image 676
Fara Avatar asked Jul 05 '12 12:07

Fara


1 Answers

You can read XML simply by casting a string to [xml]:

$xml = [xml](Get-Content foo.xml)

Then you can use

$xml.ValCurs.Valute | Where-Object {$_.ID -eq 826} | Select-Object Nominal,Value

or shorter:

$xml.ValCurs.Valute | ? {$_.ID -eq 826} | select Nominal,Value
like image 176
Joey Avatar answered Oct 05 '22 07:10

Joey