Powershell - convert XML to CSV

后端 未结 2 1737
梦谈多话
梦谈多话 2020-12-16 03:54

I was able to convert XML to CSV by using the following code:

    #read from file
[xml]$inputFile = Get-Content \"c:\\pstest\\test.xml\"
#export xml as csv
         


        
2条回答
  •  一个人的身影
    2020-12-16 04:05

    The first object (or Select-Object etc.) in a pipeline defines the header for output no matter if it's file or console output.

    What you could do is convert them to csv in two rounds and add it to the same file. Ex:

    $inputFile.Transaction.TXNHEAD | ConvertTo-Csv -NoTypeInformation -Delimiter ";" | Set-Content -Path "c:\pstest\test.csv" -Encoding UTF8
    $inputFile.Transaction.TXNDETAIL | ConvertTo-Csv -NoTypeInformation -Delimiter ";" | Add-Content -Path "c:\pstest\test.csv" -Encoding UTF8
    

    You can also combine them like this:

    $inputFile.Transaction.TXNHEAD, $x.Transaction.TXNDETAIL |
    ForEach-Object { $_ | ConvertTo-Csv -NoTypeInformation -Delimiter ";" } |
    Set-Content -Path "c:\pstest\test.csv" -Encoding UTF8
    

提交回复
热议问题