簡體   English   中英

我如何使用 powershell 列出 xml 中的所有屬性名稱

[英]how do i list all the attribute name in an xml using powershell

使用 powershell 如何獲取屬性名稱列表。

  <productSettings>
    <productGroups>
      <productGroup attributeName1="1" 
                    attributeName2="1" 
                    attributeName3="xyz" 
                    attributeName4="0"/>
    </productGroups>
  </productSettings>

當我嘗試使用 Attributes 屬性時,我得到的屬性值不是屬性的名稱

[xml]$config = Get-Content 'C:\\ProductGroup.config'

$configNodes = $config.productSettings.productGroups.ChildNodes

foreach($configNode in $configNodes)
{
    $configNode.Attributes
}

傳統方法:迭代屬性和 output 它們的名稱:

foreach ($configNode in $configNodes) {
    foreach ($attr in $configNode.Attributes) {
        $attr.Name
    }
}

PowerShell 方法:在 PowerShell 3.0+ 中,您可以在 collections 上使用點符號來訪問嵌套屬性:

foreach ($configNode in $configNodes) {
    $configNode.Attributes.Name
}

事實上,你可以 go 至

$config.productSettings.productGroups.productGroup.Attributes.Name

這將在所有<productGroup>元素中生成所有屬性名稱,即它相當於 XPath

/productSettings/productGroups/productGroup/@*/name()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM