我想转换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>
进入哈希表:
Name Value
ipAddress 10.2.1.90
gitDir C:\git\project
gitArchiveDir C:\git\archive
apacheDocroot /var/www
apacheUsername root
目前使用此读取方法:
$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path
$File = $directorypath + '\config.xml'
$CONFIG = "CONFIG"
$CFG = [xml] ( gc $File )
$CFG.test.ipAddress
答案 0 :(得分:15)
这应该可以解决问题:
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