如何循环分组的XML节点

nor*_*ert 2 xml powershell

我有这种格式的XML文档

<rss>
  <channel>
    <item>
      <id>1</id>
      <image>img_32.jpeg</image>
    </item>
    <item>
      <id>2</id>
      <image>img_42.jpeg</image>
    </item>
    <item>
      <id>1</id>
      <image>img_52.jpeg</image>
    </item>
    <item>
      <id>3</id>
      <image>img_62.jpeg</image>
    </item>
    <item>
      <id>4</id>
      <image>img_72.jpeg</image>
    </item>
  </channel>
  </rss>
Run Code Online (Sandbox Code Playgroud)

ID节点不是唯一的.我想使用PowerShell做的是按ID分组,然后遍历每个ID的分组项.

ID 1 has two images
  loop through the two images
    do something with it

ID 2 has one image
  loop through the one image
    do something with it

etc..
Run Code Online (Sandbox Code Playgroud)

到目前为止,我有以下内容:

[xml]$xml = (New-Object System.Net.WebClient).DownloadString("https://myfeedurl.xml")
$grouped = $xml.rss.channel.item | Group id
$grouped
Run Code Online (Sandbox Code Playgroud)

哪个回报

Count Name        Group
----- ----        -----
    2 1           {item}{item}
    1 2           {item}
    1 3           {item}
    1 4           {item}

但我无法想象如何使用这个分组信息以便最终得到一个独特的分组ID项列表,这样我就可以轻松地遍历图像.

Mat*_*sen 5

Group返回的每个项的属性Group-Object包含组元素 - 您只需要一个嵌套循环:

foreach($group in $grouped)
{
  "Processing images with id $($group.Name)"
  foreach($image in $group.Group)
  {
    "Processing $($image.Name)"
  }
}
Run Code Online (Sandbox Code Playgroud)