[英]Best way to assemble a string file in 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>
我將使用單個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
有很多通往羅馬的方式。 我個人喜歡使用現有的工具。 您可以使用.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>
查看此SO問題以了解使用.Net的替代方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.