当然不确定你想如何格式化你的 XML,但下面的代码应该给你一种方法来做到这一点。
$FREQUENCE_DECOMPTE = 'Nom="FREQUENCE_DECOMPTE" Valeur="MENS"'
$LIBELLE_ORGANISME = 'Nom="LIBELLE_ORGANISME" Valeur="HUMANIS CCN OG"'
$MONTANT_TOTAL = 'Nom="MONTANT_TOTAL" Valeur="0"'
$POLE = 'Nom="POLE" Valeur="1ADP"'
$CODE_ORGANISME = 'Nom="CODE_ORGANISME" Valeur="1ADP"'
# read the csv file
$liste = Import-Csv -path 'D:\Test\test.csv' -Delimiter ';'
# get the total remaining records to process
$remaining = $liste.Count
# set a maximum value of items for each resulting XML file
$maxItemsPerXml = 3
# set a xml output file counter and an item index counter
$xmlFileCount = 1
$currentItem = 0
# loop through all items
while ($remaining -gt 0) {
$itemCount = [math]::Min($maxItemsPerXml, $remaining)
$xmlItems = for ($i = 0; $i -lt $itemCount; $i++) {
$item = $liste[$i + $currentItem]
$fin = if ($item.UF6E8_CANAL -eq "ML") {1} else {0}
# parse the date from the 'UCB63_DATENUM' field
$date = [datetime]::ParseExact($item.UCB63_DATENUM, 'M/d/yy HH:mm', $null)
$RecupDateFinTraitement = $date.ToString('dd/MM/yyyy')
$RecupDateFin30 = $date.AddDays(30).ToString('dd/MM/yyyy')
# output each item in xml-style
# you can change the element names to whatever you want of course
@"
<Item>
<U6618_FILENAME>$($item.U6618_FILENAME)</U6618_FILENAME>
<Indices>
<Index $FREQUENCE_DECOMPTE></Index>
<Index $LIBELLE_ORGANISME></Index>
<Index $MONTANT_TOTAL></Index>
<Index $POLE></Index>
<Index $CODE_ORGANISME></Index>
</Indices>
<UCB63_DATENUM>$($item.UCB63_DATENUM)</UCB63_DATENUM>
<U65B8_IDRP>$($item.U65B8_IDRP)</U65B8_IDRP>
<RecupDateFinTraitement>$RecupDateFinTraitement</RecupDateFinTraitement>
<RecupDateFin30>$RecupDateFin30</RecupDateFin30>
<ALERTEMAIL>$($item.UF6E8_CANAL)</ALERTEMAIL>
<Fin>$fin</Fin>
</Item>
"@
}
# create a complete file path and name for the output xml
$xmlFile = 'D:\Test\Test_{0:D8}.xml' -f $xmlFileCount
# create the XML content, complete with declaration and root node and write it to file
@"
<?xml version="1.0" encoding="utf-8"?>
<root>
$($xmlItems -join "`r`n")
</root>
"@ | Set-Content -Path $xmlFile -Encoding UTF8
# update the counters
$xmlFileCount++
$remaining -= $itemCount
$currentItem += $itemCount
}
您可能想要修改现在使用 Here-String 模板构建 XML 的方式。我只是将 CSV 中的标题名称作为元素名称,除了您在所有大写字母中定义的索引变量。
从那些我取下Index
零件并将其设置为元素名称。
使用您的示例 CSV 输出 and$maxItemsPerXml = 3
将是 2 个名为Test_00000001.xml
and的文件Test_00000002.xml
。
测试_00000001.xml
<?xml version="1.0" encoding="utf-8"?>
<root>
<Item>
<U6618_FILENAME>457E6659_ZN_LIQRLVPR_A_V_ML.pdf</U6618_FILENAME>
<Indices>
<Index Nom="FREQUENCE_DECOMPTE" Valeur="MENS"></Index>
<Index Nom="LIBELLE_ORGANISME" Valeur="HUMANIS CCN OG"></Index>
<Index Nom="MONTANT_TOTAL" Valeur="0"></Index>
<Index Nom="POLE" Valeur="1ADP"></Index>
<Index Nom="CODE_ORGANISME" Valeur="1ADP"></Index>
</Indices>
<UCB63_DATENUM>7/8/19 22:27</UCB63_DATENUM>
<U65B8_IDRP>1367091</U65B8_IDRP>
<RecupDateFinTraitement>08-07-2019</RecupDateFinTraitement>
<RecupDateFin30>07-08-2019</RecupDateFin30>
<ALERTEMAIL>ML</ALERTEMAIL>
<Fin>1</Fin>
</Item>
<Item>
<U6618_FILENAME>49453878_ZN_LIQRLVPR_A_V_ML.pdf</U6618_FILENAME>
<Indices>
<Index Nom="FREQUENCE_DECOMPTE" Valeur="MENS"></Index>
<Index Nom="LIBELLE_ORGANISME" Valeur="HUMANIS CCN OG"></Index>
<Index Nom="MONTANT_TOTAL" Valeur="0"></Index>
<Index Nom="POLE" Valeur="1ADP"></Index>
<Index Nom="CODE_ORGANISME" Valeur="1ADP"></Index>
</Indices>
<UCB63_DATENUM>9/11/19 23:03</UCB63_DATENUM>
<U65B8_IDRP>106440</U65B8_IDRP>
<RecupDateFinTraitement>11-09-2019</RecupDateFinTraitement>
<RecupDateFin30>11-10-2019</RecupDateFin30>
<ALERTEMAIL>ML</ALERTEMAIL>
<Fin>1</Fin>
</Item>
<Item>
<U6618_FILENAME>497E585B_ZN_LIQRLVPR_A_V_CS.pdf</U6618_FILENAME>
<Indices>
<Index Nom="FREQUENCE_DECOMPTE" Valeur="MENS"></Index>
<Index Nom="LIBELLE_ORGANISME" Valeur="HUMANIS CCN OG"></Index>
<Index Nom="MONTANT_TOTAL" Valeur="0"></Index>
<Index Nom="POLE" Valeur="1ADP"></Index>
<Index Nom="CODE_ORGANISME" Valeur="1ADP"></Index>
</Indices>
<UCB63_DATENUM>9/24/19 21:04</UCB63_DATENUM>
<U65B8_IDRP>1536658</U65B8_IDRP>
<RecupDateFinTraitement>24-09-2019</RecupDateFinTraitement>
<RecupDateFin30>24-10-2019</RecupDateFin30>
<ALERTEMAIL>CS</ALERTEMAIL>
<Fin>0</Fin>
</Item>
</root>
测试_00000002.xml
<?xml version="1.0" encoding="utf-8"?>
<root>
<Item>
<U6618_FILENAME>58453B75_ZN_LIQRLVPR_A_V_ML.pdf</U6618_FILENAME>
<Indices>
<Index Nom="FREQUENCE_DECOMPTE" Valeur="MENS"></Index>
<Index Nom="LIBELLE_ORGANISME" Valeur="HUMANIS CCN OG"></Index>
<Index Nom="MONTANT_TOTAL" Valeur="0"></Index>
<Index Nom="POLE" Valeur="1ADP"></Index>
<Index Nom="CODE_ORGANISME" Valeur="1ADP"></Index>
</Indices>
<UCB63_DATENUM>2/12/20 22:12</UCB63_DATENUM>
<U65B8_IDRP>1406091</U65B8_IDRP>
<RecupDateFinTraitement>12-02-2020</RecupDateFinTraitement>
<RecupDateFin30>13-03-2020</RecupDateFin30>
<ALERTEMAIL>ML</ALERTEMAIL>
<Fin>1</Fin>
</Item>
</root>