【问题标题】:Looping through XML using PowerShell使用 PowerShell 通过 XML 循环
【发布时间】:2018-08-28 22:10:44
【问题描述】:

我正在尝试使用 PowerShell 遍历 XML,但无法获取 InnerXML 详细信息。

XML:

<script>
    <data>
        <address>
            <street>WhiteField</street>
            <zip>560100</zip>
            <city>Bangalore</city>
            <country>India</country>
            <postofficebox>BangaloreEast</postofficebox>
        </address>
    </data>
    <data>
        <address>
            <street>Gurgaon</street>
            <zip>601000</zip>
            <city>New Delhi</city>
            <country>India</country>
            <postofficebox>New Delhi West</postofficebox>
        </address>
    </data>
</script>

脚本:

[string]$FilePath = 'C:\Users\Sujeet\Desktop\UserData.xml'

try {
    [xml]$XMLInput = Get-Content -Path $FilePath
} catch {
    Write-Host "Failed to read or parse the XML File."
    exit
}

if ((Select-Xml -Xml $XMLInput -XPath "//address" | measure).Count -gt 0) {
    Select-Xml -Xml $XMLInput -XPath "//address" | foreach {
        $_.Node.InnerXML
    }
}

如何获取&lt;street&gt;&lt;city&gt;&lt;country&gt; 的值?

此外,我只想在城市为“班加罗尔”时获取详细信息。

【问题讨论】:

    标签: xml powershell


    【解决方案1】:

    InnerXml 将 XML 节点的内容作为字符串提供给您。如果您想访问嵌套元素,请不要使用它。 $_.node 是一个对象。该对象的子节点可以这样访问:

    Select-Xml -Xml $XMLInput -XPath '//address' | ForEach-Object {
        $street = $_.Node.street
        $city   = $_.Node.city
        ...
    }
    

    使用点访问自动扩展节点的值。

    要将结果限制在特定城市,请在 XPath 表达式中添加过滤器:

    $city = 'Bangalore'
    Select-Xml -Xml $XMLInput -XPath "//address[city='${city}']" | ForEach-Object {
        $street = $_.Node.street
        $city   = $_.Node.city
        ...
    }
    

    您也可以使用SelectNodes() 方法代替Select-Xml

    $city = 'Bangalore'
    $XMLInput.SelectNodes("//address[city='${city}']") | ForEach-Object {
        $street = $_.street
        $city   = $_.city
        ...
    }
    

    【讨论】:

      【解决方案2】:

      这是另一种方法:

      [xml]$xml = "<script>
          <data>
              <address>
                  <street>WhiteField</street>
                  <zip>560100</zip>
                  <city>Bangalore</city>
                  <country>India</country>
                  <postofficebox>BangaloreEast</postofficebox>
              </address>
          </data>
          <data>
              <address>
                  <street>Gurgaon</street>
                  <zip>601000</zip>
                  <city>New Delhi</city>
                  <country>India</country>
                  <postofficebox>New Delhi West</postofficebox>
              </address>
          </data>
      </script>"
      
      $addresses = $xml.script.data | Select-Object -ExpandProperty address
      foreach ($address in $addresses) {
          if($address.city -eq 'Bangalore') {
              $address.street
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-03-29
        • 1970-01-01
        • 1970-01-01
        • 2013-10-01
        • 1970-01-01
        • 2019-04-04
        • 2016-08-28
        相关资源
        最近更新 更多