1

I have a hash/array structure that I want to use to assemble a couple text files. Is this the best way to achieve this?

$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

The contents of Metadata.xml should look like this:

<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 Answers 2

4

I would use a single 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
Sign up to request clarification or add additional context in comments.

Comments

2

There are many ways to Rome. Personally I like to use the tools that exist. You got the .Net framework available, so you could do it in many ways. Ex. you could try this approach using 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>

Check out this SO question for alternative ways using .Net .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.