【问题标题】:XML to Hash Table using Powershell使用 Powershell 将 XML 转为哈希表
【发布时间】:2012-09-06 17:07:31
【问题描述】:

我要转换 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

【问题讨论】:

    标签: xml powershell hashtable


    【解决方案1】:

    这应该可以解决问题:

    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
    

    【讨论】:

      猜你喜欢
      • 2018-07-18
      • 2021-07-13
      • 2022-09-29
      • 2021-12-30
      • 1970-01-01
      • 2016-08-26
      • 1970-01-01
      • 2010-12-26
      • 1970-01-01
      相关资源
      最近更新 更多