0

次の 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 
4

1 に答える 1

0

2 つのソリューション:

  1. Select-Xml
  2. 普通の文字列連結
于 2012-12-21T22:47:17.410 に答える