次の Powershell スクリプトがあります。
$load = @(@(1, 2), @(3))
$Load | % { "[$_]" }
$Date = Get-Date "2012-01-01"
$xml = $Load | ConvertTo-Xml -NoTypeInformation
$xml.OuterXml
コードは次の結果を生成します。
[1 2]
[3]
<?xml version="1.0"?>
<Objects>
<Object>
<Property>1</Property>
<Property>2</Property>
</Object>
<Object>
<Property>3</Property>
</Object>
</Objects>
Date
ただし、xml 要素名を指定して、余分な(定数) 要素を追加できるようにしたいと考えています。それを行うのは簡単な方法ですか?
<?xml version="1.0"?>
<Groups>
<Group>
<Item><ID>1</ID><Date>2012-01-01</Date></Item>
<Item><ID>2</ID><Date>2012-01-01</Date></Item>
</Group>
<Group>
<Item><ID>3</ID><Date>2012-01-01</Date></Item>
</Group>
</Groups>
次の「新しいオブジェクト」アプローチを試しましたが、結果は非常に冗長で、私が望むものではありません。
$load = @(@(1, 2), @(3))
$Load | % { "[$_]" }
$Date = Get-Date "2012-01-01"
$xml = $Load |
% {
@(, @{ ID = $_; Date = $Date })
} |
ConvertTo-Xml -NoTypeInformation
$xml.OuterXml