【问题标题】:Convert XML to Dictionary将 XML 转换为字典
【发布时间】:2016-08-17 23:52:52
【问题描述】:

谁能告诉我如何使用 Powershell 将 xml 文件转换为字典。

这是我的 xml 文件的样子,

==>     <xml id="ATAT_Prod">
        <tag1>value1</tag1>
        <tag2>value2</tag2>
        </xml>

有人可以推荐我吗?提前致谢。

【问题讨论】:

    标签: xml powershell dictionary powershell-2.0 powershell-3.0


    【解决方案1】:

    您必须使用Get-Content cmdlet 加载内容并将其转换为[xml]。使用SelectNodescmdlet 和xpath 表达式选择所有后代并将其转换为哈希表

    $xml = [xml] (Get-Content 'YOUR_PATH_HERE')     
    $xml.SelectNodes("descendant::node()") | ? Value | % { @{$_.ParentNode = $_.Value}  }
    

    输出:

    Name                           Value                                                                                                                                                       
    ----                           -----                                                                                                                                                       
    state                          Texas                                                                                                                                                       
    environment                    Test                                                                                                                                                        
    isEnabled                      False                                                                                                                                                       
    filepath                       C:\xmlfile                                                                                                                                                  
    test                           Test environement strings                                                                                                                                   
    UAT                            UAT environment strings 
    

    编辑: 这是一个带有 dictionary 的工作示例:

    $myDictionary = New-Object "System.Collections.Generic.Dictionary``2[System.String,System.String]"
    $xml = [xml] (Get-Content 'YOUR_PATH_HERE')     
    $xml.SelectNodes("descendant::node()") | ? Value | % $myDictionary.Add($_.ParentNode.ToString(), $_.Value)  }
    

    【讨论】:

    • 非常感谢 Jisaak。它按预期工作。你能告诉我哈希表和字典不同吗?如果这些不同,你能建议我如何转换为字典
    • 不客气。 PowerShell 中没有内置字典。但是出于您的目的,哈希表也应该适合。我知道的唯一不同是哈希表不是类型安全的,而您可以指定字典以使用字符串作为键(和值)。如果需要,可以使用New-Object "System.Collections.Generic.Dictionary``2[System.String,System.String]" 创建一个.net 字典。但我怀疑你需要它。
    • Jisaak,感谢您的快速回复。其实我想试试字典。我收到语法错误。你能告诉我我在这里缺少什么吗,$xml=New-Object "System.Collections.Generic.Dictionary``2[System.String,System.String] $xml.SelectNodes("descendant::node() ") | ? 值 | % { @{$_.ParentNode = $_.Value} }.. 谢谢
    • 请问你能给我完整的脚本吗?我对更新这个有点困惑。
    • 我现在不在家,明天我会给你发一个完整的例子。我仍然会在 PowerShell 中使用哈希表。
    猜你喜欢
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 2012-12-06
    • 2010-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多