我可以使用以下代码将XML转换为CSV:
#read from file
[xml]$inputFile = Get-Content "c:\pstest\test.xml"
#export xml as csv
$inputFile.Transaction.ChildNodes | Export-Csv "c:\pstest\test.csv" -NoTypeInformation -Delimiter:";" -Encoding:UTF8
字符串
如果文件只包含一个具有一种类型的子节点的根节点,则它有效,例如:
<?xml version="1.0" encoding="UTF-8"?>
<Transaction>
<TXNDETAIL>
<RecordID>02</RecordID>
<SequenceNumber>1</SequenceNumber>
<TransactionType>01</TransactionType>
<ActionCode>01</ActionCode>
<TransactionID>17500515552017001</TransactionID>
<SellerCode>2200919TRY</SellerCode>
<BuyerCode>KOCZER</BuyerCode>
<TransactionReference> </TransactionReference>
<TransactionDescription1> </TransactionDescription1>
<TransactionDescription2> </TransactionDescription2>
<DocumentType>01</DocumentType>
<DocumentNumber>XXXXXXXXXXX</DocumentNumber>
<DocumentDate>20170301</DocumentDate>
<DocumentAmount>10000</DocumentAmount>
<CurrencyCode>949</CurrencyCode>
<TransactionAmount>10000</TransactionAmount>
<TransactionDueDate>20170505</TransactionDueDate>
<AdditionalInformation1> </AdditionalInformation1>
<AdditionalInformation2> </AdditionalInformation2>
<HashCode>XXXXXXXX</HashCode>
</TXNDETAIL>
<TXNDETAIL>
<RecordID>02</RecordID>
<SequenceNumber>2</SequenceNumber>
<TransactionType>01</TransactionType>
<ActionCode>01</ActionCode>
<TransactionID>17500515622017001</TransactionID>
<SellerCode>2200919TRY</SellerCode>
<BuyerCode>KOCZER</BuyerCode>
<TransactionReference> </TransactionReference>
<TransactionDescription1> </TransactionDescription1>
<TransactionDescription2> </TransactionDescription2>
<DocumentType>01</DocumentType>
<DocumentNumber>XXXXXXXXXXX</DocumentNumber>
<DocumentDate>20170301</DocumentDate>
<DocumentAmount>10000</DocumentAmount>
<CurrencyCode>949</CurrencyCode>
<TransactionAmount>10000</TransactionAmount>
<TransactionDueDate>20170505</TransactionDueDate>
<AdditionalInformation1> </AdditionalInformation1>
<AdditionalInformation2> </AdditionalInformation2>
<HashCode>XXXXXXXX</HashCode>
</TXNDETAIL>
<TXNDETAIL>
<RecordID>02</RecordID>
<SequenceNumber>3</SequenceNumber>
<TransactionType>01</TransactionType>
<ActionCode>01</ActionCode>
<TransactionID>17500515972017001</TransactionID>
<SellerCode>2200919TRY</SellerCode>
<BuyerCode>KOCZER</BuyerCode>
<TransactionReference> </TransactionReference>
<TransactionDescription1> </TransactionDescription1>
<TransactionDescription2> </TransactionDescription2>
<DocumentType>01</DocumentType>
<DocumentNumber>XXXXXXXXXXX</DocumentNumber>
<DocumentDate>20170301</DocumentDate>
<DocumentAmount>10000</DocumentAmount>
<CurrencyCode>949</CurrencyCode>
<TransactionAmount>10000</TransactionAmount>
<TransactionDueDate>20170505</TransactionDueDate>
<AdditionalInformation1> </AdditionalInformation1>
<AdditionalInformation2> </AdditionalInformation2>
<HashCode>XXXXXXXX</HashCode>
</TXNDETAIL>
</Transaction>
型
输出结果如下所示:
"RecordID";"SequenceNumber";"TransactionType";"ActionCode";"TransactionID";"SellerCode";"BuyerCode";"TransactionReference";"TransactionDescription1";"TransactionDescription2";"DocumentType";"DocumentNumber";"DocumentDate";"DocumentAmount";"CurrencyCode";"TransactionAmount";"TransactionDueDate";"AdditionalInformation1";"AdditionalInformation2";"HashCode"
"02";"1";"01";"01";"17500515552017001";"2200919TRY";"KOCZER";"";"";"";"01";"XXXXXXXXXXX";"20170301";"10000";"949";"10000";"20170505";"";"";"XXXXXXXX"
"02";"2";"01";"01";"17500515622017001";"2200919TRY";"KOCZER";"";"";"";"01";"XXXXXXXXXXX";"20170301";"10000";"949";"10000";"20170505";"";"";"XXXXXXXX"
"02";"3";"01";"01";"17500515972017001";"2200919TRY";"KOCZER";"";"";"";"01";"XXXXXXXXXXX";"20170301";"10000";"949";"10000";"20170505";"";"";"XXXXXXXX"
型
这很好
但是,实际上输入文件有一个“头行”信息,即TXNHEAD标记
<?xml version="1.0" encoding="UTF-8"?>
<Transaction>
<TXNHEAD>
<RecordID>01</RecordID>
<FileName>001</FileName>
<IntermediaryCode>19000033</IntermediaryCode>
<ActualizationDate>20170314</ActualizationDate>
<SequenceNumber>001</SequenceNumber>
<NumberofRecords>3</NumberofRecords>
<AmountofRecords>30000</AmountofRecords>
</TXNHEAD>
<TXNDETAIL>
<RecordID>02</RecordID>
<SequenceNumber>1</SequenceNumber>
<TransactionType>01</TransactionType>
<ActionCode>01</ActionCode>
<TransactionID>17500515552017001</TransactionID>
<SellerCode>2200919TRY</SellerCode>
<BuyerCode>KOCZER</BuyerCode>
<TransactionReference> </TransactionReference>
<TransactionDescription1> </TransactionDescription1>
<TransactionDescription2> </TransactionDescription2>
<DocumentType>01</DocumentType>
<DocumentNumber>XXXXXXXXXXX</DocumentNumber>
<DocumentDate>20170301</DocumentDate>
<DocumentAmount>10000</DocumentAmount>
<CurrencyCode>949</CurrencyCode>
<TransactionAmount>10000</TransactionAmount>
<TransactionDueDate>20170505</TransactionDueDate>
<AdditionalInformation1> </AdditionalInformation1>
<AdditionalInformation2> </AdditionalInformation2>
<HashCode>XXXXXXXX</HashCode>
</TXNDETAIL>
<TXNDETAIL>
<RecordID>02</RecordID>
<SequenceNumber>2</SequenceNumber>
<TransactionType>01</TransactionType>
<ActionCode>01</ActionCode>
<TransactionID>17500515622017001</TransactionID>
<SellerCode>2200919TRY</SellerCode>
<BuyerCode>KOCZER</BuyerCode>
<TransactionReference> </TransactionReference>
<TransactionDescription1> </TransactionDescription1>
<TransactionDescription2> </TransactionDescription2>
<DocumentType>01</DocumentType>
<DocumentNumber>XXXXXXXXXXX</DocumentNumber>
<DocumentDate>20170301</DocumentDate>
<DocumentAmount>10000</DocumentAmount>
<CurrencyCode>949</CurrencyCode>
<TransactionAmount>10000</TransactionAmount>
<TransactionDueDate>20170505</TransactionDueDate>
<AdditionalInformation1> </AdditionalInformation1>
<AdditionalInformation2> </AdditionalInformation2>
<HashCode>XXXXXXXX</HashCode>
</TXNDETAIL>
<TXNDETAIL>
<RecordID>02</RecordID>
<SequenceNumber>3</SequenceNumber>
<TransactionType>01</TransactionType>
<ActionCode>01</ActionCode>
<TransactionID>17500515972017001</TransactionID>
<SellerCode>2200919TRY</SellerCode>
<BuyerCode>KOCZER</BuyerCode>
<TransactionReference> </TransactionReference>
<TransactionDescription1> </TransactionDescription1>
<TransactionDescription2> </TransactionDescription2>
<DocumentType>01</DocumentType>
<DocumentNumber>XXXXXXXXXXX</DocumentNumber>
<DocumentDate>20170301</DocumentDate>
<DocumentAmount>10000</DocumentAmount>
<CurrencyCode>949</CurrencyCode>
<TransactionAmount>10000</TransactionAmount>
<TransactionDueDate>20170505</TransactionDueDate>
<AdditionalInformation1> </AdditionalInformation1>
<AdditionalInformation2> </AdditionalInformation2>
<HashCode>XXXXXXXX</HashCode>
</TXNDETAIL>
</Transaction>
型
当应用相同的代码时,我得到:
"RecordID";"FileName";"IntermediaryCode";"ActualizationDate";"SequenceNumber";"NumberofRecords";"AmountofRecords"
"01";"001";"19000033";"20170314";"001";"3";"30000"
"02";;;;"1";;
"02";;;;"2";;
"02";;;;"3";;
型
当我尝试用这段代码来检索头部时:
#read from file
[xml]$inputFile = Get-Content "c:\pstest\test.xml"
#export xml as csv
$inputFile.Transaction.TXNHEAD.ChildNodes | Export-Csv "c:\pstest\test.csv" -NoTypeInformation -Delimiter:";" -Encoding:UTF8
型
我得到:
"#text"
"01"
"001"
"19000033"
"20170314"
"001"
"3"
"30000"
型
我想实现的是这样的输出:
"RecordID";"FileName";"IntermediaryCode";"ActualizationDate";"SequenceNumber";"NumberofRecords";"AmountofRecords"
"01";"001";"19000033";"20170314";"001";"3";"30000"
"RecordID";"SequenceNumber";"TransactionType";"ActionCode";"TransactionID";"SellerCode";"BuyerCode";"TransactionReference";"TransactionDescription1";"TransactionDescription2";"DocumentType";"DocumentNumber";"DocumentDate";"DocumentAmount";"CurrencyCode";"TransactionAmount";"TransactionDueDate";"AdditionalInformation1";"AdditionalInformation2";"HashCode"
"02";"1";"01";"01";"17500515552017001";"2200919TRY";"KOCZER";"";"";"";"01";"XXXXXXXXXXX";"20170301";"10000";"949";"10000";"20170505";"";"";"XXXXXXXX"
"02";"2";"01";"01";"17500515622017001";"2200919TRY";"KOCZER";"";"";"";"01";"XXXXXXXXXXX";"20170301";"10000";"949";"10000";"20170505";"";"";"XXXXXXXX"
"02";"3";"01";"01";"17500515972017001";"2200919TRY";"KOCZER";"";"";"";"01";"XXXXXXXXXXX";"20170301";"10000";"949";"10000";"20170505";"";"";"XXXXXXXX"
我做错了什么?
2条答案
按热度按时间ffvjumwh1#
管道中的第一个对象(或
Select-Object
等)定义了输出的头,无论是文件还是控制台输出。你可以做的是分两轮将它们转换为csv,并将其添加到同一个文件中。
字符串
您也可以像这样合并:
型
093gszye2#
下面是我的代码:
字符串