在PowerShell中组装字符串文件的最佳方法

时间:2013-06-11 19:44:30

标签: powershell

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

$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>

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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")

的test.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>

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