title: Powershell - convert XML to CSV
body: <p>I was able to convert XML to CSV by using the following code: </p>
  
  <pre><code>    #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
  </code></pre>
  
  <p>It works if the files contain only one root node with one type of child nodes, for example:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Transaction&gt;
      &lt;TXNDETAIL&gt;
          &lt;RecordID&gt;02&lt;/RecordID&gt;
          &lt;SequenceNumber&gt;1&lt;/SequenceNumber&gt;
          &lt;TransactionType&gt;01&lt;/TransactionType&gt;
          &lt;ActionCode&gt;01&lt;/ActionCode&gt;
          &lt;TransactionID&gt;17500515552017001&lt;/TransactionID&gt;
          &lt;SellerCode&gt;2200919TRY&lt;/SellerCode&gt;
          &lt;BuyerCode&gt;KOCZER&lt;/BuyerCode&gt;
          &lt;TransactionReference&gt; &lt;/TransactionReference&gt;
          &lt;TransactionDescription1&gt; &lt;/TransactionDescription1&gt;
          &lt;TransactionDescription2&gt; &lt;/TransactionDescription2&gt;
          &lt;DocumentType&gt;01&lt;/DocumentType&gt;
          &lt;DocumentNumber&gt;XXXXXXXXXXX&lt;/DocumentNumber&gt;
          &lt;DocumentDate&gt;20170301&lt;/DocumentDate&gt;
          &lt;DocumentAmount&gt;10000&lt;/DocumentAmount&gt;
          &lt;CurrencyCode&gt;949&lt;/CurrencyCode&gt;
          &lt;TransactionAmount&gt;10000&lt;/TransactionAmount&gt;
          &lt;TransactionDueDate&gt;20170505&lt;/TransactionDueDate&gt;
          &lt;AdditionalInformation1&gt; &lt;/AdditionalInformation1&gt;
          &lt;AdditionalInformation2&gt; &lt;/AdditionalInformation2&gt;
          &lt;HashCode&gt;XXXXXXXX&lt;/HashCode&gt;
      &lt;/TXNDETAIL&gt;
      &lt;TXNDETAIL&gt;
          &lt;RecordID&gt;02&lt;/RecordID&gt;
          &lt;SequenceNumber&gt;2&lt;/SequenceNumber&gt;
          &lt;TransactionType&gt;01&lt;/TransactionType&gt;
          &lt;ActionCode&gt;01&lt;/ActionCode&gt;
          &lt;TransactionID&gt;17500515622017001&lt;/TransactionID&gt;
          &lt;SellerCode&gt;2200919TRY&lt;/SellerCode&gt;
          &lt;BuyerCode&gt;KOCZER&lt;/BuyerCode&gt;
          &lt;TransactionReference&gt; &lt;/TransactionReference&gt;
          &lt;TransactionDescription1&gt; &lt;/TransactionDescription1&gt;
          &lt;TransactionDescription2&gt; &lt;/TransactionDescription2&gt;
          &lt;DocumentType&gt;01&lt;/DocumentType&gt;
          &lt;DocumentNumber&gt;XXXXXXXXXXX&lt;/DocumentNumber&gt;
          &lt;DocumentDate&gt;20170301&lt;/DocumentDate&gt;
          &lt;DocumentAmount&gt;10000&lt;/DocumentAmount&gt;
          &lt;CurrencyCode&gt;949&lt;/CurrencyCode&gt;
          &lt;TransactionAmount&gt;10000&lt;/TransactionAmount&gt;
          &lt;TransactionDueDate&gt;20170505&lt;/TransactionDueDate&gt;
          &lt;AdditionalInformation1&gt; &lt;/AdditionalInformation1&gt;
          &lt;AdditionalInformation2&gt; &lt;/AdditionalInformation2&gt;
          &lt;HashCode&gt;XXXXXXXX&lt;/HashCode&gt;
      &lt;/TXNDETAIL&gt;
      &lt;TXNDETAIL&gt;
          &lt;RecordID&gt;02&lt;/RecordID&gt;
          &lt;SequenceNumber&gt;3&lt;/SequenceNumber&gt;
          &lt;TransactionType&gt;01&lt;/TransactionType&gt;
          &lt;ActionCode&gt;01&lt;/ActionCode&gt;
          &lt;TransactionID&gt;17500515972017001&lt;/TransactionID&gt;
          &lt;SellerCode&gt;2200919TRY&lt;/SellerCode&gt;
          &lt;BuyerCode&gt;KOCZER&lt;/BuyerCode&gt;
          &lt;TransactionReference&gt; &lt;/TransactionReference&gt;
          &lt;TransactionDescription1&gt; &lt;/TransactionDescription1&gt;
          &lt;TransactionDescription2&gt; &lt;/TransactionDescription2&gt;
          &lt;DocumentType&gt;01&lt;/DocumentType&gt;
          &lt;DocumentNumber&gt;XXXXXXXXXXX&lt;/DocumentNumber&gt;
          &lt;DocumentDate&gt;20170301&lt;/DocumentDate&gt;
          &lt;DocumentAmount&gt;10000&lt;/DocumentAmount&gt;
          &lt;CurrencyCode&gt;949&lt;/CurrencyCode&gt;
          &lt;TransactionAmount&gt;10000&lt;/TransactionAmount&gt;
          &lt;TransactionDueDate&gt;20170505&lt;/TransactionDueDate&gt;
          &lt;AdditionalInformation1&gt; &lt;/AdditionalInformation1&gt;
          &lt;AdditionalInformation2&gt; &lt;/AdditionalInformation2&gt;
          &lt;HashCode&gt;XXXXXXXX&lt;/HashCode&gt;
      &lt;/TXNDETAIL&gt;
  &lt;/Transaction&gt;
  </code></pre>
  
  <p>The output would be like:</p>
  
  <pre><code>    "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"
  </code></pre>
  
  <p>Which is great.</p>
  
  <p>However, the input file in reality has a "header line"  information, the TXNHEAD tag</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Transaction&gt;
      &lt;TXNHEAD&gt;
          &lt;RecordID&gt;01&lt;/RecordID&gt;
          &lt;FileName&gt;001&lt;/FileName&gt;
          &lt;IntermediaryCode&gt;19000033&lt;/IntermediaryCode&gt;
          &lt;ActualizationDate&gt;20170314&lt;/ActualizationDate&gt;
          &lt;SequenceNumber&gt;001&lt;/SequenceNumber&gt;
          &lt;NumberofRecords&gt;3&lt;/NumberofRecords&gt;
          &lt;AmountofRecords&gt;30000&lt;/AmountofRecords&gt;
      &lt;/TXNHEAD&gt;
      &lt;TXNDETAIL&gt;
          &lt;RecordID&gt;02&lt;/RecordID&gt;
          &lt;SequenceNumber&gt;1&lt;/SequenceNumber&gt;
          &lt;TransactionType&gt;01&lt;/TransactionType&gt;
          &lt;ActionCode&gt;01&lt;/ActionCode&gt;
          &lt;TransactionID&gt;17500515552017001&lt;/TransactionID&gt;
          &lt;SellerCode&gt;2200919TRY&lt;/SellerCode&gt;
          &lt;BuyerCode&gt;KOCZER&lt;/BuyerCode&gt;
          &lt;TransactionReference&gt; &lt;/TransactionReference&gt;
          &lt;TransactionDescription1&gt; &lt;/TransactionDescription1&gt;
          &lt;TransactionDescription2&gt; &lt;/TransactionDescription2&gt;
          &lt;DocumentType&gt;01&lt;/DocumentType&gt;
          &lt;DocumentNumber&gt;XXXXXXXXXXX&lt;/DocumentNumber&gt;
          &lt;DocumentDate&gt;20170301&lt;/DocumentDate&gt;
          &lt;DocumentAmount&gt;10000&lt;/DocumentAmount&gt;
          &lt;CurrencyCode&gt;949&lt;/CurrencyCode&gt;
          &lt;TransactionAmount&gt;10000&lt;/TransactionAmount&gt;
          &lt;TransactionDueDate&gt;20170505&lt;/TransactionDueDate&gt;
          &lt;AdditionalInformation1&gt; &lt;/AdditionalInformation1&gt;
          &lt;AdditionalInformation2&gt; &lt;/AdditionalInformation2&gt;
          &lt;HashCode&gt;XXXXXXXX&lt;/HashCode&gt;
      &lt;/TXNDETAIL&gt;
      &lt;TXNDETAIL&gt;
          &lt;RecordID&gt;02&lt;/RecordID&gt;
          &lt;SequenceNumber&gt;2&lt;/SequenceNumber&gt;
          &lt;TransactionType&gt;01&lt;/TransactionType&gt;
          &lt;ActionCode&gt;01&lt;/ActionCode&gt;
          &lt;TransactionID&gt;17500515622017001&lt;/TransactionID&gt;
          &lt;SellerCode&gt;2200919TRY&lt;/SellerCode&gt;
          &lt;BuyerCode&gt;KOCZER&lt;/BuyerCode&gt;
          &lt;TransactionReference&gt; &lt;/TransactionReference&gt;
          &lt;TransactionDescription1&gt; &lt;/TransactionDescription1&gt;
          &lt;TransactionDescription2&gt; &lt;/TransactionDescription2&gt;
          &lt;DocumentType&gt;01&lt;/DocumentType&gt;
          &lt;DocumentNumber&gt;XXXXXXXXXXX&lt;/DocumentNumber&gt;
          &lt;DocumentDate&gt;20170301&lt;/DocumentDate&gt;
          &lt;DocumentAmount&gt;10000&lt;/DocumentAmount&gt;
          &lt;CurrencyCode&gt;949&lt;/CurrencyCode&gt;
          &lt;TransactionAmount&gt;10000&lt;/TransactionAmount&gt;
          &lt;TransactionDueDate&gt;20170505&lt;/TransactionDueDate&gt;
          &lt;AdditionalInformation1&gt; &lt;/AdditionalInformation1&gt;
          &lt;AdditionalInformation2&gt; &lt;/AdditionalInformation2&gt;
          &lt;HashCode&gt;XXXXXXXX&lt;/HashCode&gt;
      &lt;/TXNDETAIL&gt;
      &lt;TXNDETAIL&gt;
          &lt;RecordID&gt;02&lt;/RecordID&gt;
          &lt;SequenceNumber&gt;3&lt;/SequenceNumber&gt;
          &lt;TransactionType&gt;01&lt;/TransactionType&gt;
          &lt;ActionCode&gt;01&lt;/ActionCode&gt;
          &lt;TransactionID&gt;17500515972017001&lt;/TransactionID&gt;
          &lt;SellerCode&gt;2200919TRY&lt;/SellerCode&gt;
          &lt;BuyerCode&gt;KOCZER&lt;/BuyerCode&gt;
          &lt;TransactionReference&gt; &lt;/TransactionReference&gt;
          &lt;TransactionDescription1&gt; &lt;/TransactionDescription1&gt;
          &lt;TransactionDescription2&gt; &lt;/TransactionDescription2&gt;
          &lt;DocumentType&gt;01&lt;/DocumentType&gt;
          &lt;DocumentNumber&gt;XXXXXXXXXXX&lt;/DocumentNumber&gt;
          &lt;DocumentDate&gt;20170301&lt;/DocumentDate&gt;
          &lt;DocumentAmount&gt;10000&lt;/DocumentAmount&gt;
          &lt;CurrencyCode&gt;949&lt;/CurrencyCode&gt;
          &lt;TransactionAmount&gt;10000&lt;/TransactionAmount&gt;
          &lt;TransactionDueDate&gt;20170505&lt;/TransactionDueDate&gt;
          &lt;AdditionalInformation1&gt; &lt;/AdditionalInformation1&gt;
          &lt;AdditionalInformation2&gt; &lt;/AdditionalInformation2&gt;
          &lt;HashCode&gt;XXXXXXXX&lt;/HashCode&gt;
      &lt;/TXNDETAIL&gt;
  &lt;/Transaction&gt;
  </code></pre>
  
  <p>When applying the same code, I get:</p>
  
  <pre><code>    "RecordID";"FileName";"IntermediaryCode";"ActualizationDate";"SequenceNumber";"NumberofRecords";"AmountofRecords"
  "01";"001";"19000033";"20170314";"001";"3";"30000"
  "02";;;;"1";;
  "02";;;;"2";;
  "02";;;;"3";;
  </code></pre>
  
  <p>When I am trying this code instead to retrieve just the head:</p>
  
  <pre><code>#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
  </code></pre>
  
  <p>I get: </p>
  
  <pre><code>"#text"
  "01"
  "001"
  "19000033"
  "20170314"
  "001"
  "3"
  "30000"
  </code></pre>
  
  <p>What I am trying to achieve, is this output:</p>
  
  <pre><code>"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"
  </code></pre>
  
  <p>What am I doing wrong? </p>