Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML to Hash Table using Powershell

I want to convert XML :

<TEST>
    <ipAddress value="10.2.1.90"/>
    <gitDir value="C:\git\project"/>
    <gitArchiveDir value="C:\git\archive"/>
    <apacheDocroot value="/var/www"/>
    <apacheUsername value="root"/>
</TEST>

Into a hash table :

Name             Value

ipAddress        10.2.1.90
gitDir           C:\git\project
gitArchiveDir    C:\git\archive
apacheDocroot    /var/www
apacheUsername   root

Currently using this read method:

$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path
$File = $directorypath + '\config.xml'
$CONFIG = "CONFIG"
$CFG = [xml] ( gc $File )

$CFG.test.ipAddress
like image 543
Dan Kanze Avatar asked Sep 13 '12 14:09

Dan Kanze


1 Answers

This should do the trick:

PS C:\> $CFG = [xml]@'
>> <TEST>
>>     <ipAddress value="10.2.1.90"/>
>>     <gitDir value="C:\git\project"/>
>>     <gitArchiveDir value="C:\git\archive"/>
>>     <apacheDocroot value="/var/www"/>
>>     <apacheUsername value="root"/>
>> </TEST>
>> '@
>>
PS C:\> $ht = @{}
PS C:\> $CFG.test.ChildNodes | Foreach {$ht[$_.Name] = $_.Value}
PS C:\> $ht

Name                           Value
----                           -----
apacheUsername                 root
gitArchiveDir                  C:\git\archive
ipAddress                      10.2.1.90
apacheDocroot                  /var/www
gitDir                         C:\git\project
like image 139
Keith Hill Avatar answered Oct 06 '22 01:10

Keith Hill