1

我有一个哈希/数组结构,我想用它来组装几个文本文件。这是实现这一目标的最佳方式吗?

$resolutions = @(
    @{"bitrate" = 1100; "width" = 1920; "height" = 1080};
    @{"bitrate" = 800; "width" = 800; "height" = 448};
    @{"bitrate" = 400; "width" = 800; "height" = 448};
    @{"bitrate" = 128; "width" = 800; "height" = 448};
    @{"bitrate" = 64; "width" = 800; "height" = 448}
)

$metadata =
@"
<xml>
    <targets>`r`n
"@

foreach ($resolution in $resolutions)
{
    $metadata += "        <target>`r`n"
    $metadata += "            <bitrate>$($resolution["bitrate"])</bitrate>`r`n"
    $metadata += "            <width>$($resolution["width"])</width>`r`n"
    $metadata += "            <height>$($resolution["height"])</height>`r`n"
    $metadata += "        </target>`r`n"
}

$metadata +=
@"
    </targets>
</xml>
"@

$metadata | out-file Metadata.xml -encoding ASCII

Metadata.xml 的内容应如下所示:

<xml>
    <targets>
        <target>
            <bitrate>1100</bitrate>
            <width>1920</width>
            <height>1080</height>
        </target>
        <target>
            <bitrate>800</bitrate>
            <width>800</width>
            <height>448</height>
        </target>
        <target>
            <bitrate>400</bitrate>
            <width>800</width>
            <height>448</height>
        </target>
        <target>
            <bitrate>128</bitrate>
            <width>800</width>
            <height>448</height>
        </target>
        <target>
            <bitrate>64</bitrate>
            <width>800</width>
            <height>448</height>
        </target>
    </targets>
</xml>
4

2 回答 2

4

我会使用一个单一的here-string:

$metadata = @"
<xml>
    <targets>
$(
    foreach ($resolution in $resolutions)
    {
        "<target>"
            "<bitrate>$($resolution.bitrate)</bitrate>"
            "<width>$($resolution.width)</width>"
            "<height>$($resolution.height)</height>"
        "</target>"
    }
)
    </targets>
</xml>
"@

$metadata | out-file Metadata.xml -encoding ASCII
于 2013-06-11T20:16:23.737 回答
2

去罗马的方式有很多。我个人喜欢使用现有的工具。您获得了可用的 .Net 框架,因此您可以通过多种方式进行操作。前任。您可以尝试使用这种方法XmlDocument

$resolutions = @(
    @{"bitrate" = 1100; "width" = 1920; "height" = 1080};
    @{"bitrate" = 800; "width" = 800; "height" = 448};
    @{"bitrate" = 400; "width" = 800; "height" = 448};
    @{"bitrate" = 128; "width" = 800; "height" = 448};
    @{"bitrate" = 64; "width" = 800; "height" = 448}
)

#Create XMLdoc
$doc = New-Object xml
#Create XML root node "xml"
$xml = $doc.AppendChild($doc.CreateElement("xml"))
#Create collectionnode for targets
$targets = $xml.AppendChild($doc.CreateElement("targets"))

#Create target-node for each resolution
foreach ($res in $resolutions) {
    $target = $doc.CreateElement("target")
    $target.AppendChild($doc.CreateElement("bitrate")).InnerText = $res["bitrate"]
    $target.AppendChild($doc.CreateElement("width")).InnerText = $res["width"]
    $target.AppendChild($doc.CreateElement("height")).InnerText = $res["height"]
    $targets.AppendChild($target) | out-null
}

$doc.Save("C:\Users\graimer\Desktop\test.xml")

测试.xml

<xml>
  <targets>
    <target>
      <bitrate>1100</bitrate>
      <width>1920</width>
      <height>1080</height>
    </target>
    <target>
      <bitrate>800</bitrate>
      <width>800</width>
      <height>448</height>
    </target>
    <target>
      <bitrate>400</bitrate>
      <width>800</width>
      <height>448</height>
    </target>
    <target>
      <bitrate>128</bitrate>
      <width>800</width>
      <height>448</height>
    </target>
    <target>
      <bitrate>64</bitrate>
      <width>800</width>
      <height>448</height>
    </target>
  </targets>
</xml>

查看这个 SO question以了解使用 .Net 的替代方法。

于 2013-06-11T20:16:37.583 回答