如何将空值 xml 元素转换为 PowerShell 字典

How to convert empty valued xml elements to PowerShell Dictionary

我正在尝试将以下 xml 文件转换为 PowerShell 字典。

<configuration>
    <environment id="Test">
        <client>ABC</client>
        <type>Test</type>
        <template> </template>
        <targetmachines>
            <targetmachine>
                <name>Any</name>
                <remotedirectory>C:\ServiceDirectory</remotedirectory>
            </targetmachine>
        </targetmachines>
        <connectionStrings>
            <DB1>User ID=xx;password=xx=;</DB1>
            <DB2>User ID=yy;password=yy=;</DB2>
        </connectionStrings>
    </environment>
</configuration>

这是我的 powershell 脚本:

$environmentId = "Test"
$configxml = [xml] (Get-Content "xmlfile")   

$keyValuePairs = New-Object "System.Collections.Generic.Dictionary``2[System.String,System.String]"
$configxml.SelectNodes("descendant::configuration/environment[@id='$($environmentId)']/descendant::text()[normalize-space()]") | Where-Object {$_.Value} |
ForEach-Object {
                $keyValuePairs.Add($_.ParentNode.ToString(), $_.Value)
                }
Write-Output $keyValuePairs

我得到以下输出:

Key                                                                      Value                                                                  
---                                                                      -----                                                                  
client                                                                   ABC                                                                    
type                                                                     Test                                                                   
name                                                                     Any                                                                    
remotedirectory                                                          C:\ServiceDirectory                                                    
DB1                                                                      User ID=xx;password=xx=;                                               
DB2                                                                      User ID=yy;password=yy=; 

上述 PowerShell 脚本运行良好。但我想要以下输出。这里的区别是我们有一个名为 "template" 的额外键,它具有空值。我想将空值元素转换为字典。

Key                                                                      Value                                                                  
---                                                                      -----                                                                  
client                                                                   ABC                                                                    
type                                                                    Test 
template                                                                                                                                      
name                                                                     Any                                                                    
remotedirectory                                                          C:\ServiceDirectory                                                    
DB1                                                                      User ID=xx;password=xx=;                                               
DB2                                                                      User ID=yy;password=yy=;

有人可以建议我如何更新我的 powershell 脚本吗?提前致谢。

这应该可以满足您的需求:

$xml = [xml]'<configuration>
    <environment id="Test">
        <client>ABC</client>
        <type>Test</type>
        <template> </template>
        <targetmachines>
            <targetmachine>
                <name>Any</name>
                <remotedirectory>C:\ServiceDirectory</remotedirectory>
            </targetmachine>
        </targetmachines>
        <connectionStrings>
            <DB1>User ID=xx;password=xx=;</DB1>
            <DB2>User ID=yy;password=yy=;</DB2>
        </connectionStrings>
    </environment>
</configuration>'
$environmentId = "Test"

$dictionary = New-Object 'System.Collections.Generic.Dictionary[string,string]'
$xml.SelectNodes("//configuration/environment[@id='$environmentId']//*[not(*)]") | `
  % { $dictionary.Add($_.Name,$_.InnerText) }

词典内容

Key                Value                   
---                -----                   
client             ABC                     
type               Test                    
template                                   
name               Any                     
remotedirectory    C:\ServiceDirectory     
DB1                User ID=xx;password=xx=;
DB2                User ID=yy;password=yy=;