gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:30:30 26 4
gpt4 key购买 nike

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

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

最佳答案

我将使用单个here字符串:

$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

关于powershell - 在PowerShell中组装字符串文件的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17052418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com