我有一个哈希/数组结构,我想用它来组装几个文本文件。这是实现这一目标的最佳方式吗?
$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>